如何调试“'我的班级'的类型初始值设定项引发了异常” [英] How to debug "The type initializer for 'my class' threw an exception"

查看:84
本文介绍了如何调试“'我的班级'的类型初始值设定项引发了异常”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了异常:我的班级的类型初始值设定项引发了异常。在运行Web应用程序后在浏览器中。由于这似乎是从视图(.aspx)生成的错误消息,因此我无法看到堆栈跟踪或任何日志记录此错误的来源。

I am getting the exception: The type initializer for 'my class' threw an exception. in my browser after running my web application. Since this seems to be an error message generated from the view (.aspx), there is no way I can see the stack trace or any log for the source of this error.

我已经在网上阅读了一些,调试的一种解决方案是抛出 TypeInitializationException ,然后查看内部异常以找出问题所在。当我不知道在哪里用try / catch包围代码时,该怎么办?

I have read a bit around the net and one solution to debugging is to throw a TypeInitializationException and then looking at the inner exception to find out what was wrong. How can I do this when I don't know where to surround code with a try/catch ?

推荐答案

由错误的静态构造函数,或由错误的静态属性/字段的内联初始化。例如:

This can be caused by a bad static constructor, or by bad inline initialization of static properties/fields. For instance:

class A
{
    static A()
    {
        //buggy code here
    }
    static SomeField f = new ThisClassThrowsWhenConstructed(); // <-- or here
}

这篇关于如何调试“'我的班级'的类型初始值设定项引发了异常”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆