How to ensure that a static constructors is called without calling any member

c#, constructor, static, static-constructor

Solution

You can use the `RuntimeHelpers.RunClassConstructor` method (assuming that I correctly understood what you were trying to do...)

RuntimeHelpers.RunClassConstructor(typeof(YourType).TypeHandle);

Problem

I have a class with a static constructor. I want the static constructor to be called without calling or using any of its members, but only if the constructor has not been called already. I tried using reflection. With reflection I can invoke the static constructor (many times), but I cannot find out if it has already been called before. How do I do this? EDIT This is not only ONE class I am talking about, it could be more. Lets say, all classes marked with a special attribute.

Original source