So if you have a class that contains static fields, there is a static method that gets called to initialize these fields at startup (before any class instances are created). If you look at the IL, the method looks like this:
You can use Type.GetConstructors(BindingFlags.Static | BindingFlags.NonPublic) and select the one where IsStatic is true.
Ah excellent! I thought I tried that already, but I just tried it again and got it working.
A related question...
Let's say I'm writing a compiler that takes IL and compiles it to some native code. At runtime, would it be enough to call all static contructors of all classes to ensure that static fields are properly initialized (does order matter?), or is there something else that needs to be executed as well (in addition to the assembly's EntryPoint)?
Call all static constructors
Calling all static constructors before calling entry point can be inefficient if there are a lot of them. More importantly, if you want to call all static constructors at startup you'll find that there's no particular ordering you can use.
The basic requirement is that the static constructor (aka type initialized) of a type is called before any of the static fields of that type are accessed. This lazy initialization generates a particular ordering that you cannot reproduce at statup without some complicated or even impossible code analysis.
In my case the number of static fields will be minimal so I don't think it should be a problem to just call them all.
Although, this lazy initialization... How would that work in cases like this:
private static DateTime initTime = DateTime.Now;
...or cases where the static field is something that does real work, like a class that creates a file in its constructor, etc? Some other class instance might need to use that file. A bit of a contrived example, but still...
@BitFlipper: Those cases are the developer's problem. Since the specification doesn't require that all static ctors are called at startup it's obvious that one should not depend on such behavior.
OK thanks, that makes sense. Basically it is a non-issue.
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.