简体   繁体   English

创建 object 时出现 TypeInitializationException 异常

[英]TypeInitializationException exception on creating an object

I have an assembly (class library project in.Net 3.5) that has some references like System.Configuration and System.Web .我有一个程序集(.Net 3.5 中的类库项目),其中包含一些参考,例如System.ConfigurationSystem.Web I use it on a web application and it works fine.我在 web 应用程序上使用它,它工作正常。

Now, I need to make a reference to a Windows Forms project and I can't understand what is happening.现在,我需要参考 Windows Forms 项目,但我不明白发生了什么。 When I try to create an instance of my class it does not work;当我尝试创建我的 class 的实例时,它不起作用; an exception of type TypeInitializationException is thrown.抛出TypeInitializationException类型的异常。

I try to create other instances of my assembly and those work, except this specific class.除了这个特定的 class 之外,我尝试创建我的程序集和这些工作的其他实例。

Does anybody know what is happening?有人知道发生了什么吗?

TypeInitializationException is usually thrown when a static field of the class can't be initialized.当无法初始化 class 的 static 字段时,通常会引发 TypeInitializationException。 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.如果 MyClass 的构造函数抛出,将在第一次使用 BadClass 之前导致 TypeInitializationException。

You can look at the InnerException property of the TypeInitializationException to drill down into the cause of the failure in more detail.您可以查看 TypeInitializationException 的 InnerException 属性,以更详细地深入了解失败的原因。 It will usually point you to the underlying exception that caused the type initialization to fail.它通常会指出导致类型初始化失败的底层异常。

TypeInitializationException is thrown when the class initializer fails.class初始化程序失败时,将引发 TypeInitializationException。 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.这可能有很多原因,但很可能您的类的 static 构造函数中有一些代码会引发异常。 You can likely look at the InnerException property to get the real exception.您可能会查看InnerException属性以获取真正的异常。

Just to catch another scenario, this error will be thrown when your AppConfig contains a section that is not defined in the configSections node.只是为了捕捉另一种情况,当您的 AppConfig 包含未在 configSections 节点中定义的部分时,将引发此错误。 It's case sensitive, so verify that your custom config sections match what's in the configSections node.它区分大小写,因此请验证您的自定义配置部分是否与 configSections 节点中的内容匹配。

For me it was duplicate key in static dictionary对我来说,它是 static 字典中的重复键

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 启动时发生Monodevelop异常:TypeInitializationException - Monodevelop exception at startup: TypeInitializationException “TypeInitializationException 未处理”异常 c# - "TypeInitializationException was unhandled" exception c# 未处理的异常:system.typeInitializationException - unhandled exception: system.typeInitializationException 未处理的类型异常(System.TypeInitializationException) - Unhandled Type Exception(System.TypeInitializationException) TypeInitializationException: &#39; 的类型初始值设定项<Module> &#39;抛出异常 - TypeInitializationException: The type initializer for '<Module>' threw an exception 在UFT上创建DotNetFactory对象的异常 - Exception on creating a DotNetFactory object on UFT System.TypeInitializationException:&#39;Tips&#39;的类型初始值设定项引发了异常 - System.TypeInitializationException: The type initializer for 'Tips' threw an exception System.TypeInitializationException: &#39;Bid&#39; 的类型初始值设定项引发异常。 - System.TypeInitializationException: 'The type initializer for 'Bid' threw an exception.' mscorlib.dll 中发生 System.TypeInitializationException 类型的未处理异常 - Unhandled exception of type System.TypeInitializationException occured in mscorlib.dll AnyStore1.exe 中出现“System.TypeInitializationException”类型的未处理异常 - An unhandled exception of type 'System.TypeInitializationException' occurred in AnyStore1.exe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM