2014-09-01 72 views
1

我想在运行时和设计时运行一些注册码。 对我来说,最好的方法似乎是使用静态构造函数,但似乎并未运行。静态构造函数在使用设计器时会被调用吗?

public class MyComponent : Component 
{ 
    static string someVar = "Static constructor not yet run"; 

    static MyComponent() 
    { 
     someVar = "Static constuctor run"; 
    } 

    public MyComponent() 
    { 
     //Do some constructor things 
    } 
} 

有没有办法让静态构造函数在设计时运行? 如果没有一个可以接受的模式来做这种事情?

回答

1

是的,无论设计时间还是运行时间,静态构造函数都会运行。一个简单的测试可以证明它。

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 
    } 
} 

现在将组件添加到设计器中,然后构建项目。您会看到消息框Static constructor run

+1

@Landerah虽然这回答你的问题,我相信这是一个[XYProblem(http://meta.stackexchange.com/questions/66377/what-是最XY-问题)。如果你能解释你想达到的目标,你可能会得到很好的答案。 – 2014-09-01 07:01:11

+0

啊我的错!我用同样的方法来测试函数是否被调用,但我没有重建我的项目。谢谢! – Landerah 2014-09-01 07:35:46

0

这就是MSDN不得不说的static constructors

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

无法直接调用静态构造函数。

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

基本上,你不能通过调用它作为普通方法来控制它。

他们是如何工作的一个例子:

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.  
    */ 
+0

感谢您的信息,但我们**知道应该在正常的构造函数之前调用一个静态构造函数,这就是我的问题所指的。 – Landerah 2014-09-01 07:38:20