为Program类抛出TypeInitializationException

时间:2014-07-25 11:32:04

标签: c# visual-studio

我的Windows窗体应用程序工作较早,但突然间它停止工作。我得到以下例外:

enter image description here

有以下例外情况:

System.TypeInitializationException was unhandled
Message: An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll
Additional information: The type initializer for 'NotificationTester.Program' threw an exception.

单击“确定”后,VS窗口将显示以下内容:

enter image description here

解决方案之前工作正常。我不知道什么是错的。

13 个答案:

答案 0 :(得分:44)

因此:Program中的任何一个字段初始值设定项或静态构造函数都失败了。找出为什么。注意:InnerException具有引发的实际异常,但是基本上:只调试字段初始值设定项和静态构造函数。因此,请查看Program类中的任何一个:

static SomeType someField = /* some non-trivial expression or method call */ 

或:

static Program() {
    // stuff
}

答案 1 :(得分:12)

另一个可能的原因:app.config有重复的部分。

答案 2 :(得分:9)

可能的原因:使用重复键初始化静态字典。

答案 3 :(得分:6)

我得到了相同的错误消息,对于我的情况,原因是我的主程序设置为32位控制台应用程序,我添加了一个访问64位dll的私有变量记录器。

string s = "10 20 3 0";
stringstream ss(s);
vector<int> arr;
string tmp;
while (ss >> tmp)
{
    arr.push_back(stoi(tmp));
}

在我将主程序更改为64位后,问题就解决了。

答案 4 :(得分:3)

就我而言,原因是<configSections>不是config文件中的第一个。

  

每个只允许一个<configSections>元素    配置文件(如果存在)必须是根<configuration>元素的第一个子节点。

configSections元素移到配置文件的顶部。

希望对某人有所帮助。

答案 5 :(得分:1)

我的平台目标设置与任何CPU相同,并且更喜欢32位选中,取消选中最后一个解决了我的问题。

答案 6 :(得分:1)

这是我在过去2小时内必须处理的一个奇怪问题。我通过从我创建的列表中删除静态来解决它。

private static readonly List<Person> someList = GlobalConfiguration.Connection.PopulateList();

这一个:

private readonly List<Person> someList = GlobalConfiguration.Connection.PopulateList();

希望它有所帮助,你不必花两个小时来找出这个错误......

答案 7 :(得分:1)

确保您没有遗漏任何依赖项DLL。在我的例子中,我引用的DLL依赖于另一个DLL。

如果发生了这种情况,请查看“内部异常”属性,然后您将看到更好的错误消息。就我而言,它说“找不到xxx.dll”

答案 8 :(得分:1)

所以,如果你没有得到像我这样的innerException消息,你可以在一些静态变量上设置一个断点,比如你可能声明的字符串。然后你可以用F10从那里调试前进。

答案 9 :(得分:0)

此问题对我来说是由在AppData \ Local [制造商] [产品名称]目录中创建的流氓user.config文件引起的。我不确定它是如何实现的,但似乎时不时地创造出来。

答案 10 :(得分:0)

在尝试了这里列出的所有答案之后,我有了一个新答案:

&#39; MyLibrary&#39;的类型初始值设定项抛出异常。

如果您在InnerException(s)中看到类似下面的内容..........

"Could not load file or assembly 'log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a' or one of its dependencies. The system cannot find the file specified.":"log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a"

就我而言,我的目录有正确的版本dll。 (在这种情况下为log4net.dll)。

然后......问题被发现了。程序集中的程序集重定向。

:(

由于我的版本正确,我删除了所有重定向。你的情况可能会有所不同。

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="log4net" publicKeyToken="b32731d11ce58905" />
        <codeBase version="1.2.9.0" href="log4netv1.2.9.0\log4net.dll" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="log4net" publicKeyToken="1b44e1d426115821" />
        <codeBase version="1.2.10.0" href="log4netv1.2.10.0\log4net.dll" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" />
        <codeBase version="1.2.13.0" href="log4netv1.2.13.0\log4net.dll" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

答案 11 :(得分:0)

我遇到了同样的问题:在MS VS 2015中运行程序并触发异常消息:

System.TypeInitializationException was unhandled
Message: An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll
Additional information: Der Typeninitialisierer für "<Module>" hat eine Ausnahme verursacht.

问题在哪里产生此异常? 因此,我启动了EXE文件并显示了异常消息对话框。使用MS VS回答“调试模式”,因此将显示有关异常的更多信息。

System.IO.FileNotFoundException was unhandled
    Message: An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
    Additional information: Die Datei oder Assembly "SIC, Version=19.2.6.3, Culture=neutral, PublicKeyToken=**************" oder eine Abhängigkeit davon wurde nicht gefunden. Das System kann die angegebene Datei nicht finden.

我转到此参考资料并分析属性 属性:“复制本地”为假-因为之前已将其安装到GAC中 解决方案:将属性“ Copy Local”设置为true并构建新的程序集并进行工作:-)

此信息可能有用!祝你有美好的一天。

答案 12 :(得分:0)

在我看来,这是一件非常愚蠢的事情。我不小心输入了这样的内容

<configuration>
    <appSettings>
         <add key="someKey" value="SomeValue" />sss
         <add key="someKey1" value="SomeValue1" />
    </appSettings>
</configuration> 

sss-为我造成了错误。任何随机字符输入。