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

时间:2014-03-31 19:24:56

标签: c# .net gettext

我有一个类库,其中包含几个属性的静态类Utils。

我将Utils类中的一个属性调用到我的控制台应用程序中,我收到此错误。 以下是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");
}

当我尝试在控制台应用程序中获取CurrentUserId属性时,我得到了异常。我注释掉了公共静态字符串RowHeader = _.T(" Some Header");代码和异常消失了。在任何一种情况下,我都没有编译器或构建错误。

" _"命名类是这一个,只有一些修改:Translation Class

我的问题是为什么CurrentUserId属性因为RowHeader属性而抛出异常?

2 个答案:

答案 0 :(得分:2)

要回答您的问题,可以在第一个类引用发生时评估所有静态成员。 (这是轻描淡写和过度简化)它取决于.NET版本,静态构造函数的存在,懒惰变量等等,但在这看起来你可能遇到问题,其中对成员的第一次引用也初始化其他成员和其中一个导致例外。

对于错误,检查类型初始值设定项异常的InnerException属性以获取确切错误的详细信息。这是错误的最佳指示。

答案 1 :(得分:1)

首先尝试设置_.ResourceCulture