简体   繁体   English

在c#中输入初始化程序异常

[英]Type initializer exception in c#

I have a class library that contains static class Utils with few properties. 我有一个类库,包含具有很少属性的静态类Utils。

I call one of the properties from the Utils class into my Console application and I get this error. 我将Utils类中的一个属性调用到我的控制台应用程序中,我收到此错误。 Here is a small sample of the Utils class. 以下是Utils类的一小部分示例。

public static class Utils
{
    public static int CurrentEmpId = -1;
    public static int CurrentUserId
    {
        get
        {
            if (HttpContext.Current != null)
            {
                 if(HttpContext.Current.Session["CurrentUserId"] == null)
                 {
                     HttpContext.Current.Session["CurrentUserId"] = GetCurrentUser();
                     return Int32.Parse(HttpContext.Current.Session["CurrentUserId"]);
                 }
                 else
                 {
                     return Int32.Parse(HttpContext.Current.Session["CurrentUserId"]);
                 }
            }

            return -1;
        }
    }
    //this is making call to a static Method in a static Class called _
    public static string RowHeader = _.T("Some Header");
}

When i try to take the CurrentUserId property in my console app i get the exception. 当我尝试在我的控制台应用程序中获取CurrentUserId属性时,我得到了异常。 I commented out the public static string RowHeader = _.T("Some Header"); 我注释掉了公共静态字符串RowHeader = _.T(“Some Header”); code and the exception was gone. 代码和异常消失了。 In either of the cases i dont have compiler or build errors. 在任何一种情况下我都没有编译器或构建错误。

The "_" named class is this one with a few modifications: Translation Class “_”命名类是这一个,只有一些修改: 翻译类

My question is why is the CurrentUserId property is throws exception because of the RowHeader one? 我的问题是为什么CurrentUserId属性因为RowHeader属性而抛出异常?

To answer your question, all Static members can be evaluated when the first class reference happens. 要回答您的问题,可以在第一个类引用发生时评估所有静态成员。 (this is an understatement and over simplification) It depends on .NET version, presence of Static constructors, Lazy variables etc. but in this it looks like you may be hitting the issue, where the first reference to a member also initializes other members and one of that causes the exception. (这是轻描淡写和过度简化)它取决于.NET版本,静态构造函数的存在,懒惰变量等等,但在这看起来你可能遇到问题,其中对成员的第一次引用也初始化其他成员和其中一个导致异常。

for the error, inspect the InnerException property of a type initializer exception to get the details of the exact error. 对于错误,请检查类型初始值设定项异常的InnerException属性以获取确切错误的详细信息。 It is the best indication of the error. 这是错误的最佳指示。

首先尝试设置_.ResourceCulture

暂无
暂无

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

相关问题 类型初始化器异常-C# - Type initializer exception - C# C#错误“…的类型初始值设定项引发了异常 - C# Error "The type initializer for … threw an exception TFS C#-加载workItemStore返回null或键入初始化器异常 - TFS C# - Loading workItemStore returns null or type initializer exception 错误:Daemon.Global的类型初始化器在c#中引发异常 - Error : The Type Initializer of Daemon.Global threw an exception in c# C#动态类型初始化程序 - C# Dynamic Type Initializer 使用c#asp.net连接到Firebird数据库会引发类型初始化器异常 - Connect to Firebird database with c# asp.net throws type initializer 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#) C#“ Google.Apis.Json.NewtonsoftJsonSerializer”的类型初始值设定项引发了异常 - C# The type initializer for 'Google.Apis.Json.NewtonsoftJsonSerializer' threw an exception C# - 'Microsoft.Exchange.WebServices.Data.ExchangeServiceBase' 的类型初始值设定项引发异常 - C# - The type initializer for 'Microsoft.Exchange.WebServices.Data.ExchangeServiceBase' threw an exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM