简体   繁体   English

错误:Daemon.Global的类型初始化器在c#中引发异常

[英]Error : The Type Initializer of Daemon.Global threw an exception in c#

I am using the below class file, where some variables are declared to use in the entire application. 我正在使用下面的类文件,其中声明了一些变量以在整个应用程序中使用。 Now I used that variable BlockLogOut in another class file to make the value true. 现在,我在另一个类文件中使用了该变量BlockLogOut来使值变为true。

I just put this below line and getting error in it.. TypeInitializationException 我只是将其放在行下方并在其中获取错误TypeInitializationException

Global.BlockLogOut = True; Global.BlockLogOut = True;

The weird thing is, it was working fine for many months and i am getting this error now on the above line. 奇怪的是,它已经工作了好几个月了,我现在在上面的行中遇到了这个错误。 Of course i was modifying some other stuffs in the application, but surely not this class file. 当然,我正在修改应用程序中的其他内容,但肯定不是此类文件。 What would have been the problem? 会是什么问题?

Inner Exception : system.null reference exception, object reference not set to an instance of an object 内部异常:system.null引用异常,对象引用未设置为对象的实例

   namespace Daemon
    {
        class Global
        {
            public static bool BlockLogOut = false;
        }
    }

This error TypeInitializationException : The exception that is thrown as a wrapper around the exception thrown by the class initializer. 此错误TypeInitializationException :作为包装初始化器引发的异常的包装器引发的异常。 This class cannot be inherited. 这个类不能被继承。

I assume the sample code is not the full code in your Global class. 我假设示例代码不是您的Global类中的完整代码。 A null reference exception indicates that you are using a class instance somewhere in your static constructor; 空引用异常表示您正在静态构造函数中某处使用类实例; it's not related to the GlobalLock variable you have included in the code since is a value type and not a class. 它与您包含在代码中的GlobalLock变量无关,因为它是值类型而不是类。

I bet that you have a static constructor :) If so, then the exception actually occurs inside the static constructor. 我打赌您有一个静态构造函数:)如果是这样,则该异常实际上发生在静态构造函数内部。

namespace Daemon
{
    using System;

    public class Global
    {
        public static bool BlockLogOut = false;

        static Global()
        {
            throw new Exception();
        }
    }
}

namespace ConsoleApplication
{
    public class Program
    {
        static void Main(string[] args)
        {
            Daemon.Global.BlockLogOut = true; // TypeInitializationException
        }
    }
}

Have you changed your target framework to .NET 4.0 recently? 您最近是否将目标框架更改为.NET 4.0? Because this may be due to Type initialization changes in .NET 4.0 (Jon Skeet has a post about it on his blog). 因为这可能是由于键入.NET 4.0初始化变化 (乔恩斯基特有一个帖子关于它在自己的博客)。

Long story short, there has been some changes about how and when a type will be initialized by the CLR. 长话短说,CLR初始化类型和方式的方式已经有所改变。 While it still conforms to the specification, it does so in a lazier fashion. 尽管它仍然符合规范,但它以懒惰的方式进行。 Again, in a nutshell, it won't initialize fields until it actually needs them (I'm not too sure about the peculiarities of these changes though). 再次概括地说,它不会初始化字段,直到它真正需要它们为止(尽管我不太确定这些更改的特殊性)。

Make sure you don't depend on BlockLogOut being initialized in some sort of empty Initialize() { } method. 确保您不依赖在某种空的Initialize() { }方法中Initialize() { } BlockLogOut It won't actually be initialized until a method that uses it gets called (and yes, that's applicable to constructors and instance methods too; As long as they don't use that particular field). 在使用它的方法被调用之前,它实际上不会初始化(是的,它也适用于构造函数和实例方法;只要它们不使用该特定字段)。

But as always, you are guarantied that the field will be initialized before you need it and that this initialization will happen at most once. 但是与往常一样,您可以确保在需要该字段之前对其进行初始化,并且该初始化最多会发生一次。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#错误“…的类型初始值设定项引发了异常 - C# Error "The type initializer for … threw an exception 从C#错误调用Matlab函数错误:“ tes.tambah”的类型初始值设定项引发了异常 - Call Matlab Function from C# error : The type initializer for 'tes.tambah' threw an exception ClosedXML 读取 excel 文件错误:“MS.Utility.EventTrace”的类型初始化程序引发异常。 (C#) - ClosedXML Read excel file Error: The type initializer for 'MS.Utility.EventTrace' threw an exception. (C#) 错误'Emgu.CV.CvInvoke'的类型初始值设定项引发了C#,Opencv和EmguCv的异常 - Error The type initializer for 'Emgu.CV.CvInvoke' threw an exception with C#, Opencv and EmguCv 类型初始值设定项引发异常 - Type initializer threw an exception ''的类型初始值设定项引发了异常 - The type initializer for '' threw an exception 类型初始值设定项引发了异常 - The type initializer threw an exception 如何修复错误“'的类型初始化程序?' 抛出异常。” 使用 EASendMail 通过 C# 发送电子邮件时? - How do I fix the error “The type initializer for '?' threw an exception.” when sending e-mails through C# with EASendMail? C#“ Google.Apis.Json.NewtonsoftJsonSerializer”的类型初始值设定项引发了异常 - C# The type initializer for 'Google.Apis.Json.NewtonsoftJsonSerializer' threw an exception “ crystaldecisions.shared.sharedutils”的类型初始值设定项引发了异常。 在C#中 - The type initializer for 'crystaldecisions.shared.sharedutils' threw an exception. in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM