基类中的静态字段生命周期
我有带有单个静态字段的简单基类。我有许多从这个基类派生的类。当我创建派生类时,它会导致调用基类静态构造函数来初始化静态字段(按预期工作)。问题是,当我创建另一个从相同基类继承的派生类时,基类中的静态字段仍然为空,为什么???它是由我实例化的第一个类初始化的。
基类中的静态字段不应该具有全局分配并且对所有派生类可见(即共享)吗?
我的模型:(
class Base<T>
{
protected static object s_field = null;
static Base { s_field = new object(); }
}
class Derived1<T> : Base<T>
{
}
class Derived2<T> : Base<T>
{
}
// ... later in the program
Derived1<int> instance1 = new Derived1<int>(); // initializes static field (s_field in base class) for all derived types
Derived2<double> instance2 = new Derived2<double>(); // the static field is null
我可以通过调试器看到这一点,但它应该还没有被上一行初始化吗?)
I have simple base class with single static field. I have numerous classes that derive from this base class. When I create a derived class, it causes invocation of the base classes static ctor which initializes the static field (Works as expected). The problem is that when I create another derived class, that inherits from same base, the static field in the base is still null, why???? It was initialized by the first class I instantiated.
Should not static fields in base classes have global allocation and be visible (ie. shared) to all derived classes?
My model:
class Base<T>
{
protected static object s_field = null;
static Base { s_field = new object(); }
}
class Derived1<T> : Base<T>
{
}
class Derived2<T> : Base<T>
{
}
// ... later in the program
Derived1<int> instance1 = new Derived1<int>(); // initializes static field (s_field in base class) for all derived types
Derived2<double> instance2 = new Derived2<double>(); // the static field is null
(I can see this through the debugger, but should it not already have been initialized by previous line??)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
既然您已经更改了代码,我相信您需要了解泛型在 .NET 中的工作原理。
泛型中的静态行为与正常情况下略有不同。对于您提供的每个唯一的开放类型 T,基类维护唯一的静态成员值。
您可以通过 Derived double 为同一基类创建另一个开放类型 double 实例。双>然后你就会明白我所说的概念。
这里有一个示例代码可以更清楚地演示:
在这里您将只看到静态 Ctor 的 2 次调用。
Since you have changed your code i believe you need to understand how generics works in .NET.
Static in generics behaves a bit different than in normal cases. For each unique open type T you provide, the base class maintains unique static member value.
You create another instance of open type double for the same base class via Derived < double > then youll see the concept what i am talking about.
Here a sample code to demonstrate more clearly :
Here you shall see only 2 calls for the static Ctor.
我认识到自己的错误了!哇,基类实际上是一个模板类:
Base
。当我像这样创建基础对象时new Derived()
,new Derived()
,new Derived
I realized my mistake! Wow, the base class is actually a template class:
Base<T>
. When I create object of the base like thisnew Derived<int>()
,new Derived<double>()
,new Derived<object>()
, these are completely different types and therefore the static field rules are different, my understanding is that the static field will be allocated for family of type T.I have corrected the example above to reflect this (in the initial post).
当您将泛型放入图片中时,整个问题就会改变。您对静态成员继承的理解在没有泛型的情况下按预期工作,并且当泛型就位时,该概念仍然有效,但泛型在运行时创建不同的类型。
Base
和Derived1
共享相同的静态成员,其中Derived1
与是不同的类型运行时的 Base
不与其共享静态成员。Whole question changes when you put generics into the picture. Your understanding on inheritance of static members works as expected without generics and when Generics are in places, still the concept is valid with the exception that, Generics creates different types at run time.
Base<int>
andDerived1<int>
share the same static member where asDerived1<decimal>
would be a different type thanBase<int>
at run time which doesn't share the static member with.