C#泛型静态构造函数 [英] C# Generic Static Constructor

查看:233
本文介绍了C#泛型静态构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将在泛型类的静态构造函数来运行所有类型的传递到通用参数,像这样的:

 类SomeGenericClass< T> 
{
静态列表< T> _someList;
静态SomeGenericClass()
{
_someList =新的List< T>();
}
}



是否有任何借鉴的背上用这种方法?


解决方案

是的,静态构造函数将被调用一次为每个封闭类类型(如果指定类型参数创建的类型)。请参阅 C#3规范,§10.12静态构造函数。




对于一个封闭类类型的静态构造函数在给定的应用程序域中执行最多一次。




和也:




由于静态构造函数是每个封闭构造类类型执行一次,它是执行上不能在编译时通过限制要检查的类型参数运行时检查一个方便的地方(§10.1.5)。例如,下面的类型使用静态构造函数来执行该类型参数是一个枚举:




 类根< T>其中T:(!typeof运算(T).IsEnum)结构
{
静态根(){
如果{
抛出新的ArgumentException(T必须是一个枚举);
}
}
}



这也是相关阅读§4.4.2打开和关闭类型:




在运行时,所有的泛型类型声明中的所有代码在上下文中执行那是通过将类型参数的泛型声明中创建了一个封闭构造类型。泛型类型中的每个类型参数被绑定到特定的运行时类型。所有语句和表达式的运行时处理总是与封闭类型发生,并且编译时处理过程中仅发生开路类型。



每个封闭构造类型都有自己的一套静态变量,不与任何其他封闭构造类型的共享。




这程序,你可以自己运行表明,静态构造函数被称为三次,不只是一次:

 公共类节目
{
类SomeGenericClass< T>
{
静态SomeGenericClass()
{
Console.WriteLine(typeof运算(T));
}
}

类巴兹{}

静态无效的主要(字串[] args)
{
SomeGenericClass< INT>富=新SomeGenericClass<&诠释GT;();
SomeGenericClass<串GT;巴=新SomeGenericClass<串GT;();
SomeGenericClass<&巴兹GT;巴兹=新SomeGenericClass<&巴兹GT;();
}
}



输出:

 
System.Int32
System.String
计划+巴兹


Will a static constructor on a generic class be run for every type you pass into the generic parameter such as this:

 class SomeGenericClass<T>
 {
      static List<T> _someList;
      static SomeGenericClass()
      {
          _someList = new List<T>();
      }
 }

Are there any draw backs to using this approach?

解决方案

Yes, the static constructor will be called once for each closed class type (the type created when you specify the type parameters). See the C# 3 specification, §10.12 Static constructors.

The static constructor for a closed class type executes at most once in a given application domain.

and also:

Because the static constructor is executed exactly once for each closed constructed class type, it is a convenient place to enforce run-time checks on the type parameter that cannot be checked at compile-time via constraints (§10.1.5). For example, the following type uses a static constructor to enforce that the type argument is an enum:

class Gen<T> where T: struct
{
    static Gen() {
        if (!typeof(T).IsEnum) {
            throw new ArgumentException("T must be an enum");
        }
    }
}

It is also relevant to read §4.4.2 Open and closed types:

At run-time, all of the code within a generic type declaration is executed in the context of a closed constructed type that was created by applying type arguments to the generic declaration. Each type parameter within the generic type is bound to a particular run-time type. The run-time processing of all statements and expressions always occurs with closed types, and open types occur only during compile-time processing.

Each closed constructed type has its own set of static variables, which are not shared with any other closed constructed types.

This program that you can run yourself demonstrates that the static constructor is called three times, not just once:

public class Program
{
    class SomeGenericClass<T>
    {
        static SomeGenericClass()
        {
            Console.WriteLine(typeof(T));
        }
    }

    class Baz { }

    static void Main(string[] args)
    {
        SomeGenericClass<int> foo = new SomeGenericClass<int>();
        SomeGenericClass<string> bar = new SomeGenericClass<string>();
        SomeGenericClass<Baz> baz = new SomeGenericClass<Baz>();
    }
}

Output:

System.Int32
System.String
Program+Baz

这篇关于C#泛型静态构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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