什么是保证基类的静态构造函数最好的办法是叫什么名字? [英] What's the best way to ensure a base class's static constructor is called?

查看:230
本文介绍了什么是保证基类的静态构造函数最好的办法是叫什么名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在静态构造函数C#中的文档说:

The documentation on static constructors in C# says:

一个静态构造函数是用来   初始化任何静态数据,或给   执行,需要一个特定的操作   一次只执行。这就是所谓的   之前的第一个自动   实例被创建或任何静态   成员引用

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.

(约当它被自动调用),这最后一部分把我一个循环;直到看完这部分我的认为的,通过简单地以任何方式访问类的 的,我可以肯定的是它的基类的静态构造函数被调用。测试和检查文档已经表明这不是这种情况;似乎静态构造函数的基础的类是不能保证运行,直到其成员的基类中的专门的访问。

That last part (about when it is automatically called) threw me for a loop; until reading that part I thought that by simply accessing a class in any way, I could be sure that its base class's static constructor had been called. Testing and examining the documentation have revealed that this is not the case; it seems that the static constructor for a base class is not guaranteed to run until a member of that base class specifically is accessed.

现在,我想在大多数情况下,当你处理一个派生类中,您将构建一个实例,这就构成了基类的一个实例被创建,因此静态构造函数被调用。但是,如果我只是处理的的静态的成员的来源的类,然后呢?

Now, I guess in most cases when you're dealing with a derived class, you would construct an instance and this would constitute an instance of the base class being created, thus the static constructor would be called. But if I'm only dealing with static members of the derived class, what then?

为了使这一点更具体的,我觉得的code以下将工作:

To make this a bit more concrete, I thought that the code below would work:

abstract class TypeBase
{
    static TypeBase()
    {
        Type<int>.Name = "int";
        Type<long>.Name = "long";
        Type<double>.Name = "double";
    }
}

class Type<T> : TypeBase
{
    public static string Name { get; internal set; }
}

class Program
{
    Console.WriteLine(Type<int>.Name);
}

我认为访问类型&lt; T&GT; 类将自动调用静态构造 TypeBase ;但这似乎不是这样。 类型&lt; INT&GT;。.Name点,和code以上输出空字符串

I assumed that accessing the Type<T> class would automatically invoke the static constructor for TypeBase; but this appears not to be the case. Type<int>.Name is null, and the code above outputs the empty string.

除了创造一些模拟部件(如静态初始化()方法,什么也不做),有没有更好的方法,以确保基本类型的静态构造函数将被调用它的任何派生类型在使用前?

Aside from creating some dummy member (like a static Initialize() method that does nothing), is there a better way to ensure that a base type's static constructor will be called before any of its derived types is used?

如果没有,那么......模型构件是!

If not, then... dummy member it is!

推荐答案

下面的规则是非常复杂的,之间CLR 2.0和CLR 4.0它们实际上<一href="http://msmvps.com/blogs/jon_skeet/archive/2010/01/26/type-initialization-changes-in-net-4-0.aspx">changed在微妙而有趣的方式,海事组织做出最聪明的方法脆CLR版本之间。一个初始化()方法的的可能不会做的工作在CLR 4.0,如果它不触及各个领域。

The rules here are very complex, and between CLR 2.0 and CLR 4.0 they actually changed in subtle and interesting ways, that IMO make most "clever" approaches brittle between CLR versions. An Initialize() method also might not do the job in CLR 4.0 if it doesn't touch the fields.

我会找一个替代设计,或者使用的常规的在你的类型延迟初始化(即查了一下或引用(对),看它是否已被完成)。

I would look for an alternative design, or perhaps use regular lazy initialization in your type (i.e. check a bit or a reference (against null) to see if it has been done).

这篇关于什么是保证基类的静态构造函数最好的办法是叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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