35

I have an assembly (class library project in .Net 3.5) that has some references like System.Configuration and System.Web. I use it on a web application and it works fine.

Now, I need to make a reference to a Windows Forms project and I can't understand what is happening. When I try to create an instance of my class it does not work; an exception of type TypeInitializationException is thrown.

I try to create other instances of my assembly and those work, except this specific class.

Does anybody know what is happening?

1
  • 1
    Does that class have a static constructor that is throwing an exception? Commented Aug 10, 2011 at 20:09

4 Answers 4

73

TypeInitializationException is usually thrown when a static field of the class can't be initialized. For example:

class BadClass
{
    private static MyClass fieldName = new MyClass();
}

Will cause a TypeInitializationException prior to the first usage of BadClass if the constructor for MyClass throws.

You can look at the InnerException property of the TypeInitializationException to drill down into the cause of the failure in more detail. It will usually point you to the underlying exception that caused the type initialization to fail.

Sign up to request clarification or add additional context in comments.

4 Comments

It doesn't have to be a field, it can be anything in the static constructor.
Thanks man, I forget it, I was initializing a static field that was looking some configuration ni .config file and now I add this configuration and it works for me. Cheers!
@driis: That's true; but I figure by the time you've gotten to the point you're writing a static constructor, you're "advanced" enough that you hopefully understand the consequences of it failing. Whereas simply initializing a static field is something that's common and simple enough that it's a feature that gets used without deeper understanding of the lifecycle of a class, which is why I called it out specifically as a possible cause.
Thanks for the hint! I was receiving this issue for half an hour while testing a Xamarin.Android application.
10

TypeInitializationException is thrown when the class initializer fails. There can be a number of reasons to this, but most likely you have some code in your class' static constructor, that throws an exception. You can likely look at the InnerException property to get the real exception.

Comments

4

Just to catch another scenario, this error will be thrown when your AppConfig contains a section that is not defined in the configSections node. It's case sensitive, so verify that your custom config sections match what's in the configSections node.

2 Comments

Thanks... this might very well have been the answer too -- it was for me!
Similarly if the app.config is incorrectly formed XML.
1

For me it was duplicate key in static dictionary

public static Dictionary<string, int> Cities = new Dictionary<string, int>(){
{"New York", 1},
{"Amsterdam", 2},
{"New York", 1}
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.