TypeInitializationException thrown for Program cla

2019-01-19 10:00发布

My Windows Forms application was working earlier, however suddenly it stopped working. I am getting following exception:

enter image description here

With exception details as follows:

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.

When I click OK, the VS windows then shows following:

enter image description here

The solution was working fine earlier. I don't get whats going wrong.

11条回答
Ridiculous、
2楼-- · 2019-01-19 10:34

In my case the reason was, that <configSections> wasn't first one in config file.

Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element.

Move the configSections element to the top in your config file.

Hope it helps to someone.

查看更多
祖国的老花朵
3楼-- · 2019-01-19 10:34

This issue for me was caused by a rogue user.config file that was created in the AppData\Local[Manufacturer][Product Name] directory. I'm not sure how it got there, but seems to be created every now and then.

查看更多
时光不老,我们不散
4楼-- · 2019-01-19 10:38

This is one weird issue I had to deal with for the last 2 hours. I solved it by removing the static from the lists I created.

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

with this one:

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

Hope it helps and you don't have to spend two hours to find out the bug...

查看更多
乱世女痞
5楼-- · 2019-01-19 10:39

After trying all of the answers listed here, I have a new one:

The type initializer for 'MyLibrary' threw an exception.

If you see something like the below in the 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"

In my case, my directory had the correct version dll. (log4net.dll in this case).

And then ... the issue was found. Assembly redirects in the app.config.

:(

Since I had the correct version, I removed all my redirects. Your situation may be different.

  <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>
查看更多
劫难
6楼-- · 2019-01-19 10:40

Another possible reason: the app.config has duplicate sections.

查看更多
登录 后发表回答