【发布时间】:2014-03-31 19:24:55
【问题描述】:
我有一个类库,其中包含具有少量属性的静态类 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 而引发异常?
【问题讨论】:
-
您能否提供充分的理由说明您的问题与相关stackoverflow.com/questions/1226188/… 等许多类似问题不重复? (如果静态构造函数/归档初始化程序抛出比整个类型初始化失败)
-
您的“少量修改”可能会引起您的兴趣...
-
@Dunken 修改是类的名称更改,它已更改为静态。不知道为什么会导致崩溃。