TypeInitializationException - What, Why, and How to fix
So what is a TypeInitializationException?
"The exception that is thrown as a wrapper around the exception thrown by the class initializer"
So what is a "class initializer"?
A static constructor
But I don't have a static constructor on my class.
If you are working in ASP.NET with Pages or User Controls, yes you do. The framework generates one for you.
So, basically, when you get this exception, it is because another exception was thrown while setting up a class instance, along with its default values.
Notice that the class IS being set up. The class exists, and the constructor was in the process of being invoked.
Basically, it means that something went wrong during the setup, like a failure in assigning a new class instance to a member variable. Such as in:
private MyClass m_MyClass = new MyClass();
Why does it happen?
OutOfMemoryException, NullReferenceException, MissingMethodException. I've seen all of these.
Usually it is a symptom of there being a mismatch between two or more assemblies.
This can mean that either you have (for instance) upgraded one of a set of DLLs, but not one it is dependent on.
Or
In the case of ASP.NET, it may mean that the shadow copying of the assemblies had an issue, and the cached compiled versions have not updated correctly.
In the former case, you have to hunt down the version mismatch and correct it.
In the latter, you need to get a copy of the DLLs from the site bin folder. Delete the site copies. Upload the copies you made back into the site bin folder.