How to call a static constructor

the code:

class Base<T,U> where T:Base<T,U>,new()  where U :class
{
    protected static U _val = null;
    internal static void ShowValue()
    {
        if(_val == null)new T(); //Without this line, it won't work as expected
        Console.WriteLine (_val);
    }
    internal static void Virtual()
    {
        Console.WriteLine ("Base");
    }
}
class Deriv :Base<Deriv,string>
{
    static Deriv()
    {
        _val = "some string value";
    }
    internal static new void Virtual ()
    {
        Console.WriteLine ("Deriv");
    }
}
 public static void Main (string[] args)
{
    Deriv.ShowValue();            
    Deriv.Virtual();
}

Thanks to .NET generics, I can create a bunch of specific classes that reuse common static methods defined in the base base class. It can imitate inheritance polymorphism to some extent. But to initialize different versions of static fields, I have to use static constructors. Unfortunately, we cannot call them directly, so we need to figure out a way to call it. The above example showed the way. But I do not like the creation, nor the approach to thinking. We also cannot restrict the static method of the general parameter. So I would like to ask if there is another way to do such a job!

Thanks in advance!

~~~~~~~~~~~~~~~~~

Conclusion (maybe a little early):

, . . , .cctors , , new() - , ctor.

, .cctors , . , !

    class MyClass 
    {
        static int _val = 0;
        static MyClass()
        {
            _val++;
            Console.WriteLine (_val);
        }
    }
    public static void Main (string[] args)
    {
        ConstructorInfo ci = typeof(MyClass).TypeInitializer;
        ci.Invoke(new object[0]);
        ci.Invoke(new object[0]);
        ci.Invoke(new object[0]);
    }
//result:
//1
//1
//1
//1
+5
5

. " " - .NET.

, , "" , , , .

Deriv ( ) . , Deriv.ShowValue()

Base<Deriv, string>.ShowValue();

... Deriv. , .

: ( ) - .NET 4.5, , . . .

+6

, constuctor, , .

, . - , , , - .

+2

- (= ) :

typeof(T).TypeInitializer.Invoke(null, null);

null s. MemberAccessException.

, :

internal static void ShowValue()
{
    if (_val == null)
    {
        if (typeof(T).TypeInitializer != null)
            typeof(T).TypeInitializer.Invoke(null, null);
        if (_val == null)
            throw new InvalidOperationException(string.Format("The type initializer of {0} did not initialize the _val field.", typeof(T)));
    }
    Console.WriteLine(_val);
}

And with that you can remove the restriction new().

+2
source

Static constructors automatically, only once. You cannot call them yourself.

An example from here:

public class Bus
{
    // Static constructor:
    static Bus()
    {
        System.Console.WriteLine("The static constructor invoked.");
    }    

    public static void Drive()
    {
        System.Console.WriteLine("The Drive method invoked.");
    }
}

class TestBus
{
    static void Main()
    {
        Bus.Drive();
    }
}

output:

The static constructor invoked.

The Drive method invoked.
0
source

I am directing you to the MSDN article on Static Constructors and about 10% down the page:

The static constructor is called automatically to initialize the class before creating the first instance or any static link members.

0
source

All Articles