C# 如何调试“‘我的课程’的类型初始值设定项引发异常”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12305648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:48:23  来源:igfitidea点击:

How to debug "The type initializer for 'my class' threw an exception"

c#asp.netexceptionexception-handling

提问by JF Beaulieu

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.

我收到异常:The type initializer for 'my class' threw an exception.在运行我的 Web 应用程序后在我的浏览器中。由于这似乎是从视图 (.aspx) 生成的错误消息,因此我无法查看堆栈跟踪或此错误来源的任何日志。

I have read a bit around the net and one solution to debugging is to throw a TypeInitializationExceptionand 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 ?

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

采纳答案by spender

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
}

回答by petro.sidlovskyy

This exception is thrown when the static constructor of 'my class' crashes. Please put your breakpoint there.

当“我的类”的静态构造函数崩溃时会抛出此异常。请把你的断点放在那里。

回答by Thanatos

There is something that when wrong in your static constructor of the class, and instead of throwing that error, the CLRwill throw TypeInitializationException.

当您的类的静态构造函数出错时,CLR会出现一些错误,而不是抛出该错误,而是将抛出TypeInitializationException.

回答by kombsh

Finally I found reason for this issue is AppConfig settings of my project. Yes, I have two C# projects, Project1and Project2.

最后我发现这个问题的原因是我的项目的 AppConfig 设置。是的,我有两个 C# 项目Project1Project2

Project1is contains the Static class MyDetails

Project1包含静态类MyDetails

public static MyDetails
{
  public static int _LogLevel = Int32.Parse(ConfigurationManager.AppSettings["LogLevel"])

  public static GetData()
   {
     ----code----
     ----code----
   }
}

I have following appConfig settings in Project1

我在 Project1 中有以下 appConfig 设置

<appSettings>
    <add key="LogLevel" value="5"/>
</appSettings>

The function MyDetails.GetData()is being called From Project2which is the project I am debugging now. Since Project2is the target project, the line ConfigurationManager.AppSettings["LogLevel"]will try to read setting LogLevel from Project2. but LogLevelsetting is only available in Project1. so, we need to add appsettings in Project2.

函数MyDetails.GetData()正在从Project2调用,这是我现在正在调试的项目。由于Project2是目标项目,行ConfigurationManager.AppSettings["LogLevel"]将尝试从Project2读取设置 LogLevel 。但LogLevel设置仅在Project1 中可用。所以,我们需要在Project2 中添加 appsettings 。

The issue The type initializer for threw an exception has been solved after adding the folowing appsettings in appConfig of Project2,

Project2 的appConfig 中添加以下 appsettings 后,The type initializer for throw an exception 问题已解决,

<appSettings>
    <add key="LogLevel" value="5"/>
</appSettings>