使用设计器时调用Do静态构造函数

本文关键字:调用 Do 静态 构造函数 | 更新日期: 2023-09-27 18:20:07

我想在运行时和设计时运行一些注册代码。对我来说,最好的方法似乎是使用静态构造函数,但它似乎没有运行。

public class MyComponent : Component 
{
    static string someVar = "Static constructor not yet run";
    static MyComponent() 
    {
        someVar = "Static constuctor run";
    }
    public MyComponent()
    {
        //Do some constructor things
    }
}

有没有一种方法可以让静态构造函数在设计时运行?如果没有,做这种事情是否有一种公认的模式?

使用设计器时调用Do静态构造函数

是的,静态构造函数将运行,而与设计时或运行时无关。一个简单的测试就能证明这一点

public class MyComponent : Component
{
    static string someVar = "Static constructor not yet run";
    static MyComponent()
    {
        someVar = "Static constructor run";
        MessageBox.Show(someVar);
    }
    public MyComponent()
    {
        //Do some constructor things
    }
}

现在将组件添加到设计器中,然后构建项目。您将看到消息框,其中包含消息静态构造函数运行

这就是MSDN对静态构造函数的看法:

会自动调用静态构造函数来初始化类在创建第一个实例或任何静态成员之前引用。

不能直接调用静态构造函数。

用户无法控制何时在中执行静态构造函数程序。

基本上,您不能通过像调用普通方法那样调用代码来控制它。

它们如何工作的一个例子:

    public class Bus
    {
        // Static variable used by all Bus instances. 
        // Represents the time the first bus of the day starts its route. 
        protected static readonly DateTime globalStartTime;
        // Property for the number of each bus. 
        protected int RouteNumber { get; set; }
        // Static constructor to initialize the static variable. 
        // It is invoked before the first instance constructor is run. 
        static Bus()
        {
            globalStartTime = DateTime.Now;
            // The following statement produces the first line of output,  
            // and the line occurs only once.
            Console.WriteLine("Static constructor sets global start time to {0}",
                globalStartTime.ToLongTimeString());
        }
        // Instance constructor. 
        public Bus(int routeNum)
        {
            RouteNumber = routeNum;
            Console.WriteLine("Bus #{0} is created.", RouteNumber);
        }
        // Instance method. 
        public void Drive()
        {
            TimeSpan elapsedTime = DateTime.Now - globalStartTime;
            // For demonstration purposes we treat milliseconds as minutes to simulate 
            // actual bus times. Do not do this in your actual bus schedule program!
            Console.WriteLine("{0} is starting its route {1:N2} minutes after global start time {2}.",
                                    this.RouteNumber,
                                    elapsedTime.TotalMilliseconds,
                                    globalStartTime.ToShortTimeString());
        }
    }
    class TestBus
    {
        static void Main()
        {
            // The creation of this instance activates the static constructor.
            Bus bus1 = new Bus(71);
            // Create a second bus.
            Bus bus2 = new Bus(72);
            // Send bus1 on its way.
            bus1.Drive();
            // Wait for bus2 to warm up.
            System.Threading.Thread.Sleep(25);
            // Send bus2 on its way.
            bus2.Drive();
            // Keep the console window open in debug mode.
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
    }
    /* Sample output:
        Static constructor sets global start time to 3:57:08 PM.
        Bus #71 is created.
        Bus #72 is created.
        71 is starting its route 6.00 minutes after global start time 3:57 PM.
        72 is starting its route 31.00 minutes after global start time 3:57 PM.      
   */