TypeInitializationException

TypeInitializationException

本文介绍了在类型初始值设定项中调试异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态类,没有静态构造函数,但是有很多静态成员。

I have a static class with no static constructor, but many static members. I throw a TypeInitializationException when the class is first used.

我打破了CLR异常,并且禁用了仅我的代码。

I am breaking on CLR exceptions, and I have disabled 'Just My Code'.

问题是,我仅在首次使用此类时才得到此异常;我对失败的静态字段初始化程序不屑一顾。我当然可以删除静态成员,直到找到罪魁祸首,但是有一种更简单的方法吗?

The problem is that I only get this exception when this class is first used; I don't break at the failing static field initializer. I could certainly remove the static members until I find the culprit, but is there an easier way?

有人对调试静态字段初始化有任何提示吗?

Does anyone have any tips for debugging static field initializations?

推荐答案

单击调试,异常(或按Ctrl + D,E),并告诉Visual Studio在引发任何异常时中断。然后,在引发InnerException并包装到TypeInitializationException中之前,它会中断,并且会在引发异常的行上中断。

Click Debug, Exceptions, (or press Ctrl+D, E) and tell Visual Studio to break whenever any exception is thrown. It will then break when the InnerException is thrown, before it gets wrapped in a TypeInitializationException, and it will break on the line that threw the exception.

或者,尝试在每个静态初始值设定项上设置一个断点。最后一击是引发异常的那一击。

Or, try setting a breakpoint on every static initializer; the last one hit is the one that threw the exception.

这篇关于在类型初始值设定项中调试异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 12:36