I've recently moved a project I'm working on from .NET 3.5 to .NET 4. I'm using C#, Managed C++ and Unmanaged C++.
In one of my Managed C++ (interop) I'm having a static constructor:
public ref class StaticPool : public BaseStaticPools
static Poolable^ Dequeue()
static void InitializePools()
In .NET, type initializers may only be called the first time a field is accessed. This is controlled by the
I filed a bug report, which is only available to beta testers, despite being marked "Public".
Here's the explanation from Microsoft, which you may find helpful:
For C++, this is the intended behavior. We mark our classes with
BeforeFieldInit, and so the initialization the CLR is performing is correct. We do not offer a way in C++/CLI to change this behavior. If you require the class constructor to run, you are able to call
Since we're invoking the standard here, the line from Partition I, 8.9.5 says this:
If marked BeforeFieldInit then the type’s initializer method is executed at, or sometime before, first access to any static field defined for that type.
That section actually goes into detail about how a language implementation can choose to prevent the behavior you're describing. C++/CLI chooses not to, rather they allow the programmer to do so if they wish.
Basically, since the code below has absolutely no static fields, the JIT is completely correct in simply not invoking static class constructors.