There is a class

public class A { public A() { } static A() { Console.WriteLine("Call Static Constructor A"); } public static A CreateA(Type typeA) { System.Reflection.ConstructorInfo constructorInfo = typeA.GetConstructor(new Type[0]); Console.WriteLine("Call CreateA Constructor"); A a = (A)constructorInfo.Invoke(new object[0]); return a; } } public class B : A { static B() { Console.WriteLine("Call Static Constructor B"); } public B() : base() { Console.WriteLine("Call Constructor B"); } } 

There is a code

 A a = A.CreateA(typeof(B)); 

Console output

Call Static Constructor A

Call CreateA Constructor

Call Static Constructor B

Call constructor b

Why Call CreateA Constructor is called before Call Static Constructor B and how to make it so that any type B , even typeof(B) or typeof(B).GetConstructor called static B() , but it turns out that public B() : base() is executed before static B()

UPD: And is it possible to take all types of classes inherited from the current class?


UPD: A crutch that calls the first static property of all heirs of class A to force a static constructor of each of them ...

 static static A() { Type[] allAssignableTypes = (from domainAssembly in AppDomain.CurrentDomain.GetAssemblies() from assemblyType in domainAssembly.GetTypes() where typeof(A).IsAssignableFrom(assemblyType) select assemblyType).ToArray(); foreach (Type type in allAssignableTypes) { PropertyInfo p = type.GetProperties().FirstOrDefault(x => x.GetMethod.IsStatic); if (p != null) p.GetValue(null); } } 
  • one
    You do something strange - usually, this order of calling static constructors is correct. - Pavel Mayorov
  • @PavelMayorov is quite difficult for me to describe the logic of class interaction. It was necessary for me that all static constructors dependent on A were executed ... - Dmitry Chistik
  • one
    In the general case, this is impossible - after all, an assembly containing a dependent type may not even be loaded yet. - Pavel Mayorov
  • @PavelMayorov my crutch worked ... Now with any call to something in class A in the static constructor, all static heirs are called if they have static properties. In principle, what I needed was - Dmitry Chistik

2 answers 2

MSDN: A static constructor is created or any static members are referenced.

This means that a static constructor is called once before the first creation of an object of a class, or when accessing static members of a class.

When you typeof , neither the one nor the other happens, so the static constructor is not called.

How to make so that at any appeal to type B , even typeof(B) or typeof(B).GetConstructor caused static B() ?

Apparently: no way.

  • those. the only way to force it, for example, to create a singleton and then work on classes? - Dmitry Chistik
  • @DmitryChistik, you can refer to a static property or create an object. I don’t understand why it is needed at all, maybe you are trying to screw the nail in with a screwdriver and for your real task it isn’t necessary at all - Grundy
  • It's hard for me to describe the goal I want to achieve, but yes, I twist the nail with a screwdriver ... Is it possible to get all types of classes inherited from class A ? - Dmitry Chistik
  • one
    @Dmitry Chistik, as far as I know, no, you can only get all types at all, and using Type.IsAssignableFrom check whether an object of the specified class can be assigned to the object being scanned. Well, the English question: Get all the derived types of a type - Grundy

Call typeof(B) not enough to trigger type initialization. This behavior is defined by the C # specification (section 10.12), so it cannot be changed:

For the closed-loop application type. The Constructor is a triggered case.

  • An instance of the class type is created.
  • Any type of static members are referenced.