First, I do understand that static initializers can be called in different sequences... that the only guarantee is that they will be executed prior to the first use of any member of that class.
What I am saying is that in Release Mode of .NET 4.x that guarantee is being violated.
During auto-generated deserialization code for class X, it calls the default constructor for class X, and in that code it is accessing a static member that is not yet initialized!
This never happened with .NET 3.5; and it never happens in Debug Mode in .NET 4.x (at least not yet). But in Release Mode in .NET 4.x it happens fairly consistently.
Questions:
(1) Is there a compiler flag or setting that will turn off the optimization that .NET 4.x added that evaluates static initializers more lazily??
(2) Is there a way to setup Debug mode to do all the optimizations (except the static initializer one)?
(3) Is there some #pragma or directive that we can put in the class to cause its initializers to run less lazy?
(4) I saw one mention that static constructors might affect the lazy evaluation of static initializers, but it was unclear whether they were over-relying on static initializer ordering. Does anyone know if going through every class with static initializers and adding a static contractor will solve this problem?
(5) Any other suggestions on how to fix this? (Going back to .NET 3.5 is highly undesirable because we need to move to 64b and we do NOT want to lose Edit & Continue.)