TypeInitializationException exception on creating an object

前端 未结 4 2042
天涯浪人
天涯浪人 2020-12-01 17:31

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 applica

相关标签:
4条回答
  • 2020-12-01 18:19

    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.

    0 讨论(0)
  • 2020-12-01 18:22

    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.

    0 讨论(0)
  • 2020-12-01 18:26

    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.

    0 讨论(0)
  • 2020-12-01 18:27

    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}
    };
    
    0 讨论(0)
提交回复
热议问题