Question

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.

Was it helpful?

Solution

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

RuntimeHelpers.RunClassConstructor(typeof(YourType).TypeHandle);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top