C# 什么时候在访问同一类的静态构造函数之前访问该类的静态成员

C# 什么时候在访问同一类的静态构造函数之前访问该类的静态成员,c#,class,static,static-variables,static-constructor,C#,Class,Static,Static Variables,Static Constructor,我有一个公共类和两个静态成员,我想使用静态构造函数设置这些静态成员的值。但是,即使在静态构造函数初始化它之前,从.aspx页访问的成员也是如此 任何关于如何防止这种情况并使构造函数在始终之前被命中的输入 为此添加一个小代码参考: public class test { public string var1 ; public string Description; public string var2; public str

我有一个公共类和两个静态成员,我想使用静态构造函数设置这些静态成员的值。但是,即使在静态构造函数初始化它之前,从.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 
公共类测试
{
公共字符串var1;
公共字符串描述;
公共字符串var2;
公共字符串var3;
公共静态列表MasterFeatureList=新列表();
静态测试()
{
尝试
{
if(MasterFeatureList.Count()==0)
{
使用(IM5Context context=new M5Context())
{
主功能列表=
新的
FeatureRepository(context).GetAll().Where(x=>x.Enabled==true.ToList();
}
}
}
catch(System.Exception-ex)
{
掷骰子;
}
}
公共静态字典功能=新建字典()
{
{
Feature.Values.xyz,
新测试{var1=MasterFeatureList.Find(x=>x.Id==(int)Feature.Values.xyz).Name,Description=“”,var2=“xyz”,var3=“xyz”}
},
//我有多个其他功能需要初始化

上面的代码有一个静态构造函数,它有一个静态成员,我在静态字典中使用它来初始化值。但是在静态构造函数初始化它之前,会访问该字典。

为了处理上述情况,而不是使用构造函数初始化静态主列表,我添加了一个explicit调用静态函数,该函数将初始化静态主列表,然后可以检查主列表是否有一个使用该值的值

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" }
    },
公共静态字符串集合名(int-featureId)
{
使用(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))
{
返回MasterFeatureList.Find(x=>x.Id==featureId).Name;
}
其他的
{
返回新的FeatureRepository(context).GetById(featureId).Name;
}                
}
}
公共静态只读词典功能=新建词典()
{
{
Feature.Values.xyz,
新测试{ServiceName=SetName((int)Feature.Values.xyz),Description=“”,KbUrl=“xyz”,TemplateAppendix=“xyz”}
},

您有什么证据证明这种情况正在发生?静态构造函数明显在任何静态成员之前运行,因此,如果这是一个CLR错误,我希望它已经出现(并已修复)已经存在。您可以为静态构造函数添加代码吗?它们是显然未初始化的成员,以及调用它的aspx代码?我添加了代码以供参考,在aspx中,var1被引用为test.Features[this.FeatureValue].var1,它会抛出对象引用null