문제

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.

도움이 되었습니까?

해결책

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

RuntimeHelpers.RunClassConstructor(typeof(YourType).TypeHandle);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top