Here are some helpful steps for finding the root cause of this problem... Click Debug--> Exceptions and check ON all the Thrown checkboxes. This will cause the debugger to stop on all first chance exceptions and will help you find the error under the Type Initializer error that you're seeing.
Most commonly, a TypeInitializationException exception is thrown when a static constructor is unable to instantiate a type. The InnerException property indicates why the static constructor was unable to instantiate the type.
Type Initializers are a new language construct that allow for C# (and VB using similar syntax) to shortcut creating of custom constructors or writing additional code to initialize properties.
Check the InnerException
property of the TypeInitializationException
; it is likely to contain information about the underlying problem, and exactly where it occurred.
This problem can be caused if a class tries to get value of a key in web.config or app.config which is not present there.
e.g.
The class has a static variable
private static string ClientID = System.Configuration.ConfigurationSettings.AppSettings["GoogleCalendarApplicationClientID"].ToString();
But the web.config doesn't contain the GoogleCalendarApplicationClientID
key
The error will be thrown on any static function call or any class instance creation
The type initializer for 'CSMessageUtility.CSDetails' threw an exception.
means that the static constructor on that class threw an Exception - so you need to look either in the static constructor of the CSDetails class, or in the initialisation of any static members of that class.
I ran into the same problem, when I was using a static methods in a Util class, just like you had used CSMessageUtility.CSDetails
.
The problem was that during the static initialization of the class (using the static constructor), the framework also initialize the the static variables (fields) in the class. I had a static variable that attempts to read values from app.config
, and app.config
was missing the respective settings, thus resulting in an un-handled exception. This resulted in getting the
"Object reference not set to an instance of an object."
as the inner exception.
One other thing to check when these initialize errors are thrown would be to check if the target .NET version is installed on the server. You can right click the project and see what .NET version the application is targeting.
This can happen if you have a dependency property that is registered to the wrong owner type (ownerType argument).
Notice SomeOtherControl
should have been YourControl
.
public partial class YourControl
{
public bool Enabled
{
get { return (bool)GetValue(EnabledProperty); }
set { SetValue(EnabledProperty, value); }
}
public static readonly DependencyProperty EnabledProperty =
DependencyProperty.Register(nameof(Enabled), typeof(bool), typeof(SomeOtherControl), new PropertyMetadata(false));
}
I've had the same problem caused by having two of the same configuration properties (which matches the app.config):
[ConfigurationProperty("TransferTimeValidity")]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With