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条回答
我只想做你的唯一
2楼-- · 2019-01-19 10:16

A possible reason: init a static dictionary with duplicated keys.

查看更多
何必那么认真
3楼-- · 2019-01-19 10:19

I've got the same error with platform target set as any CPU and prefer 32-bit checked, unchecking that last one solved my problem.

查看更多
看我几分像从前
4楼-- · 2019-01-19 10:22

I got the same error message and for my case the reason is my main program is set to build as 32 bit console app and I added a private variable logger which access a 64bit dll.

public class Program
{
    public static readonly Logger logger = new Logger(typeof(Program));

After I changed the main program to be build as 64bit, the issue is fixed.

查看更多
狗以群分
5楼-- · 2019-01-19 10:25

So, If you didn't get an innerException message like me, you can set a breakpoint on some of the static variables like strings that you may have declared. Then you can debug forwards from there with F10.

查看更多
何必那么认真
6楼-- · 2019-01-19 10:27

So: either one of the field-initializers, or the static constructor, for Program - is failing. Find out why. Note: the InnerException has the actual exception that was raised, but basicaly: just debug the field initializers and static constructor. So look inside the Program class for either:

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

or:

static Program() {
    // stuff
}
查看更多
淡お忘
7楼-- · 2019-01-19 10:30

Make sure you are not missing any dependency DLLs. In my case, a DLL I was referencing had a dependency on another DLL.

If this is what happened, look at the "Inner Exception" property and then you will see a better error message. In my case, it said "Cannot find xxx.dll"

查看更多
登录 后发表回答