有没有办法强制静态字段在C#进行初始化? [英] Is there a way to force static fields to be initialized in C#?

查看:132
本文介绍了有没有办法强制静态字段在C#进行初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的代码:



<预类=郎-CS prettyprint-覆盖> 类节目
{
静态程序(){
Program.program1.Value = 5;
}

静态列表<程序>值=新的List<程序>();
int值;
int值
{
{返回值; }
集合{
THIS.VALUE =价值;
Program.values.Add(本);
}
}

静态程序程序1 =新节目{值= 1};
静态程序Program2中=新节目{值= 2};
静态程序program3 =新节目{值= 3};

静态无效的主要(字串[] args)
{
如果(Program.values.Count == 0)Console.WriteLine(空);
的foreach(在Program.values VAR值)
Console.WriteLine(value.Value);
Console.ReadKey();
}
}



它打印数量只有5,如果删除了在静态构造函数代码,它打印空。



有没有办法强制静态字段进行初始化,甚至是否尚未使用?



我需要有回报简称类型的所有实例命名值的静态属性。



我试过这个代码的一些变化和。一些作品对于某些类型的,但不会为别人



编辑:上面的示例坏了,试试这个:



<预类=郎-CS prettyprint-覆盖> 类的子类< T> {
静子类()
{
值=新的List<&子类LT; T>>();
}
公共子类()
{
如果(Values.Any(I =>!i.Value.Equals(THIS.VALUE)))
{
Values.Add(本);
}
}

公众的T值{搞定;组; }

公共静态列表<&子类LT; T>>值{搞定;私人集; }
}

级超类:子类< INT>
{
公共静态超类SuperclassA1 =新的超类{值= 1};
公共静态超类SuperclassA2 =新的超类{值= 2};
公共静态超类SuperclassA3 =新的超类{值= 3};
公共静态超类SuperclassA4 =新的超类{值= 4};
}

类节目
{
静态无效的主要(字串[] args)
{
//Console.WriteLine(Superclass。 SuperclassA1); //取消注释该行和它的作品
的foreach(在Superclass.Values VAR值)
{
Console.WriteLine(value.Value);
}
Console.ReadKey();
}
}


解决方案

的回答你的问题是嗯,是的。 ,而是逼迫的两种方式之一是你已经在做的事情。



在语言规范的相关部分的10.11静态构造函数和具体为:



一类的静态构造函数在给定的应用程序域中执行最多一次静态构造函数的执行是由第一下列事件的一个应用领域内发生触发:




  • 类的一个实例被创建。

  • 任何类的静态成员的参考。



如果一个类包含在程序开始执行,该类的静态构造函数执行的主要方法是调用之前,如果一个类包含任何静态Main方法(3.1节)

通过初始化领域,这些初始化在文本顺序执行静态构造函数之前立即执行。

Consider the following code:

class Program
{
    static Program() {
        Program.program1.Value = 5;
    }

    static List<Program> values = new List<Program>();
    int value;
    int Value
    {
        get { return value; }
        set { 
            this.value = value;
            Program.values.Add(this);
        }
    }

    static Program program1 = new Program { value = 1 };
    static Program program2 = new Program { value = 2 };
    static Program program3 = new Program { value = 3 };

    static void Main(string[] args)
    {
        if (Program.values.Count == 0) Console.WriteLine("Empty");
        foreach (var value in Program.values)
            Console.WriteLine(value.Value);
        Console.ReadKey();
    }
}

It prints only the number 5, and if removed the code in the static constructor, it prints "Empty".

Is there a way to force static fields to be initialized even whether not used yet?

I need to have a static property named Values with returns all instances of the referred type.

I tried some variations of this code and some works for some types but doesn't for others.

EDIT: THE SAMPLE ABOVE IS BROKEN, TRY THIS ONE:

class Subclass<T> {
    static Subclass()
    {
        Values = new List<Subclass<T>>();
    }
    public Subclass()
    {
        if (!Values.Any(i => i.Value.Equals(this.Value)))
        {
            Values.Add(this);
        } 
    }

    public T Value { get; set; }

    public static List<Subclass<T>> Values { get; private set; }
}

class Superclass : Subclass<int>
{
    public static Superclass SuperclassA1 = new Superclass { Value = 1 };
    public static Superclass SuperclassA2 = new Superclass { Value = 2 };
    public static Superclass SuperclassA3 = new Superclass { Value = 3 };
    public static Superclass SuperclassA4 = new Superclass { Value = 4 }; 
}

class Program
{
    static void Main(string[] args)
    {
        //Console.WriteLine(Superclass.SuperclassA1); //UNCOMMENT THIS LINE AND IT WORKS
        foreach (var value in Superclass.Values)
        {
            Console.WriteLine(value.Value);
        }
        Console.ReadKey();
    }
}

解决方案

The answer to your question is 'well, yes'. But one of the two ways of "forcing" it is what you're already doing.

The relevant section in the language spec is 10.11 Static constructors, and specifically:

"The static constructor for a class executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain:

  • An instance of the class is created.
  • Any of the static members of the class are referenced.

If a class contains the Main method (Section 3.1) in which execution begins, the static constructor for that class executes before the Main method is called. If a class contains any static fields with initializers, those initializers are executed in textual order immediately prior to executing the static constructor."

这篇关于有没有办法强制静态字段在C#进行初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆