Explicitly call static constructor

我们两清 提交于 2019-12-31 17:49:13

问题


I want to write unit test for below class.
If name is other than "MyEntity" then mgr should be blank.
Negative Unit test
Using Manager private accessor I want to change name to "Test" so that mgr should be null. And then will verify the mgr value. To achieve this, I want to explicitly call the static constructor but when I call the static constructor using

Manager_Accessor.name = "Test"
typeof(Manager).TypeInitializer.Invoke(null, null); 

name is always set to "MyEntity" how to set name to "Test" and invoke the static constructor.

public class Manager
{        
        private static string name= "MyEntity";

        private static object mgr;

        static Manager()
        {
            try
            {
                mgr = CreateMgr(name);
            }
            catch (Exception ex)
            {
                mgr=null;
            }
        }
}

回答1:


As I found out today, the static constructor CAN be called directly:

from another Stackoverflow post

The other answers are excellent, but if you need to force a class constructor to run without having a reference to the type (ie. reflection), you can use:

Type type = ...;
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(type.TypeHandle);  

I had to add this code to my application to work around a possible bug in the .net 4.0 CLR.




回答2:


If you have a static member in your class (there must be, otherwise static constructor wouldn't do too much) then no need to explicitly call the static constructor.

Simply access the class where you would like to call its static constructor. E.g.:

public void MainMethod()
{
    // Here you would like to call the static constructor

    // The first access to the class forces the static constructor to be called.
    object temp1 = MyStaticClass.AnyField;

    // or
    object temp2 = MyClass.AnyStaticField;
}



回答3:


For anyone finding this thread and wondering... I just did the test. It appears System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor() will only run the static constructor if it has not already been run for another reason.

For example, if your code isn't positive whether or not previous code may have accessed the class and triggered the static constructor to run, it doesn't matter. That previous access will have triggered the static constructor to run, but then RunClassConstructor() will not run it also. RunClassConstructor() only runs the static constructor if it hasn't already been run.

Accessing the class after RunClassConstructor() also does not result in the static constructor being run a second time.

This is based on testing in a Win10 UWP app.




回答4:


Just add public static void Initialize() { } method to your static class and call it when you want. This is very similar to call constructor, because static constructor will be called automatically.




回答5:


Put the code that you'd like to call in the constructor into a public method and call it from the constructor



来源:https://stackoverflow.com/questions/11520829/explicitly-call-static-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!