class 的静态成员何时在同一个 class 的静态构造函数之前被访问
When does a static member of a class get accessed before the static constructor of the same class
我有一个 public class 和两个静态成员,我想使用静态构造函数设置这些静态成员的值。但是甚至在静态构造函数初始化它之前就可以从 .aspx 页面访问该成员。
任何关于如何防止这种情况并让构造函数总是被命中的输入。
为此添加一个小代码参考:
public class test
{
public string var1 ;
public string Description;
public string var2;
public string var3;
public static List<Feature> MasterFeatureList = new List<Feature>();
static test()
{
try
{
if (MasterFeatureList.Count() == 0)
{
using (IM5Context context = new M5Context())
{
MasterFeatureList =
new
FeatureRepository(context).GetAll().Where(x => x.Enabled == true).ToList();
}
}
}
catch (System.Exception ex)
{
throw ex;
}
}
public static Dictionary<Feature.Values, test> Features = new Dictionary<Feature.Values, test>()
{
{
Feature.Values.xyz,
new test { var1 = MasterFeatureList.Find(x=>x.Id==(int) Feature.Values.xyz).Name, Description = "", var2 = "xyz", var3 = "xyz" }
},
// i have multiple other feature to be initilaized
上面的代码有一个静态构造函数,它有一个静态成员,我在静态字典中使用它来初始化值。但是字典在静态构造函数初始化它之前就被访问了。
为了处理上述情况,而不是使用构造函数来初始化静态主列表,我添加了对静态函数的显式调用,它将初始化静态主列表,然后可以检查主列表是否有值使用那
public static string SetName(int featureId)
{
using (IM5Context context = new M5Context())
{
if (MasterFeatureList.Count() == 0)
{
MasterFeatureList = new xyzRepository(context).GetAll().Where(x => x.Enabled == true).ToList();
}
if (MasterFeatureList.Any(x => x.Id == featureId))
{
return MasterFeatureList.Find(x => x.Id == featureId).Name;
}
else
{
return new FeatureRepository(context).GetById(featureId).Name;
}
}
}
public static readonly Dictionary<Feature.Values, test> Features = new Dictionary<Feature.Values, test>()
{
{
Feature.Values.xyz,
new test { ServiceName = SetName((int)Feature.Values.xyz), Description = "", KbUrl = "xyz", TemplateAppendix = "xyz" }
},
我有一个 public class 和两个静态成员,我想使用静态构造函数设置这些静态成员的值。但是甚至在静态构造函数初始化它之前就可以从 .aspx 页面访问该成员。
任何关于如何防止这种情况并让构造函数总是被命中的输入。
为此添加一个小代码参考:
public class test
{
public string var1 ;
public string Description;
public string var2;
public string var3;
public static List<Feature> MasterFeatureList = new List<Feature>();
static test()
{
try
{
if (MasterFeatureList.Count() == 0)
{
using (IM5Context context = new M5Context())
{
MasterFeatureList =
new
FeatureRepository(context).GetAll().Where(x => x.Enabled == true).ToList();
}
}
}
catch (System.Exception ex)
{
throw ex;
}
}
public static Dictionary<Feature.Values, test> Features = new Dictionary<Feature.Values, test>()
{
{
Feature.Values.xyz,
new test { var1 = MasterFeatureList.Find(x=>x.Id==(int) Feature.Values.xyz).Name, Description = "", var2 = "xyz", var3 = "xyz" }
},
// i have multiple other feature to be initilaized
上面的代码有一个静态构造函数,它有一个静态成员,我在静态字典中使用它来初始化值。但是字典在静态构造函数初始化它之前就被访问了。
为了处理上述情况,而不是使用构造函数来初始化静态主列表,我添加了对静态函数的显式调用,它将初始化静态主列表,然后可以检查主列表是否有值使用那
public static string SetName(int featureId)
{
using (IM5Context context = new M5Context())
{
if (MasterFeatureList.Count() == 0)
{
MasterFeatureList = new xyzRepository(context).GetAll().Where(x => x.Enabled == true).ToList();
}
if (MasterFeatureList.Any(x => x.Id == featureId))
{
return MasterFeatureList.Find(x => x.Id == featureId).Name;
}
else
{
return new FeatureRepository(context).GetById(featureId).Name;
}
}
}
public static readonly Dictionary<Feature.Values, test> Features = new Dictionary<Feature.Values, test>()
{
{
Feature.Values.xyz,
new test { ServiceName = SetName((int)Feature.Values.xyz), Description = "", KbUrl = "xyz", TemplateAppendix = "xyz" }
},