Explicitly call static constructor

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

Answers


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.


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

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.


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


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.


Need Your Help

Scala: Best way to filter & map in one iteration

scala dictionary collections filter collect

I'm new to Scala and trying to figure out the best way to filter & map a collection. Here's a toy example to explain my problem.

Plotting a graph in R and exporting it to Excel in a loop

r excel plot graph ggplot2

In my data I have multiple countries and each country has 5 products. I performed a regression for each combination and now I am trying to plot the predicted values vs. the actual values on the same