I'm not sure why this works, but as far as I know (using Skeet ), if I have a static class
public static class Statics1 { public static string Value1 { get; set; } static Statics1() { Console.WriteLine("Statics1 cctor"); Value1 = "Initialized 1"; } }
The code:
Type staticType = typeof (Statics1); staticType.TypeInitializer.Invoke(null); or staticType.TypeInitializer.Invoke(new object[0]);
will throw an exception, because somehow it solves for the .ctor, and not for the .cctor class.
If I use an explicitly static class, it is considered as an abstract closed class, so the exception is that the abstract class cannot be created, and if I use a regular class with a static constructor, the exception is that the type initializer is not called back.
But if I use Invoke overload with two parameters (instance, params), for example:
Type staticType = typeof (Statics1); staticType.TypeInitializer.Invoke(null, null);
explicitly indicating that I am invoking a static method (that the value of the first null is no instance == static), this works and initializes the class.
However, static constructors are strange beasts. Calling one in this way will invoke the static constructor, even if it is already executed , that is, this code:
Console.WriteLine(Statics1.Value1); Type staticType = typeof (Statics1); staticType.TypeInitializer.Invoke(null, null);
will invoke the static constructor twice. Therefore, if your clicks have potentially important side effects, such as creating files, opening databases, etc., you can reconsider this approach.
Also, although I prefer static constructors for readability, in terms of performance, field initializers are slightly faster than static constructors