简体   繁体   English

c#静态构造函数未从派生类调用

[英]c# static constructor not called from derived class

class Bus<T>
{
    static Bus()
    {
        foreach(FieldInfo fi in typeof(T).GetFields())
        {
            if(fi.FieldType == typeof(Argument))
            {
                fi.SetValue(typeof(T), new Argument("busyname", "busyvalue"));
            }
        }
    }
}
class Buss : Bus<Buss>
{
    public static Argument field;
}

Any ideas how to make this work so that a reference to the static field in Buss triggers the static constructor in Bus? 任何想法如何使这个工作,以便Buss中的静态字段的引用触发总线中的静态构造函数?

The fact that this matters to you probably means that you are using static constructors wrong. 这对你很重要的事实可能意味着你使用静态构造函数是错误的。

With that in mind, you could make a static constructor in Buss that manually invokes the static constructor in Bus . 考虑到这一点,您可以在Buss中创建一个静态构造函数,手动调用Bus的静态构造函数。 Note that it's not possible to run a static constructor more than once. 请注意,不可能多次运行静态构造函数。

MSDN says that 'Static constructors are not inherited'. MSDN说 “静态构造函数不是继承的”。 I guess this is similar to static fields which are not inherited either. 我想这类似于静态字段,它们也没有被继承。

The static constructor of a generic type is invoked exactly once per Type , when that type is referenced. 当引用该类型时,每个Type只调用一次泛型类型的静态构造函数。

Calling Buss x = new Buss() will invoke the static constructor of Bus<Buss> . 调用Buss x = new Buss()将调用Bus<Buss>的静态构造函数。

Calling Bus<Buss> x = new Bus<Buss>() will also invoke the static constructor of Bus<Buss> , but it will do so for it's type argument Buss , setting Buss.field . 调用Bus<Buss> x = new Bus<Buss>()也将调用Bus<Buss>的静态构造函数,但它将为其类型参数Buss ,设置Buss.field

If you create a class Bugs : Bus<Buss> it will never set Bugs.field , as it will first resolve the type argument Buss , which invokes the static constructor of it's base class Bus<Buss> , setting Buss.field . 如果你创建一个class Bugs : Bus<Buss>它将永远不会设置Bugs.field ,因为它将首先解析类型参数Buss ,它调用它的基类Bus<Buss>的静态构造函数,设置Buss.field When it tries to call the static constructor of Bugs base class, it will think it had already invoked the static Bus<Buss> constructor and skip it. 当它试图调用Bugs基类的静态构造函数时,它会认为它已经调用了静态Bus<Buss>构造函数并跳过它。

Basically if I copy paste your code, create a dummy Argument class and create a new instance of Buss , the static constructor is invoked and Buss.field is set to an instance of Argument , but I do recognize some strange behavoir here in which I'd have to advise not to use reflection from a static method to reach subclasses' statics. 基本上,如果我复制粘贴代码,创建一个虚拟的Argument类,并创建一个新的实例Buss ,静态构造函数调用, Buss.field 设置为实例Argument ,但我在这里认识了一些奇怪的behavoir在我d必须建议不要使用静态方法的反射来达到子类的静态。

The example you provided only works because Buss is the type argument for itself. 您提供的示例仅起作用,因为Buss是其自身的类型参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# - 从其他类的 static 构造函数访问字段时,不调用 Static 初始化程序 - C# - Static initializer is not called when field is accessed from other class' static constructor 在每个请求上调用C#ASP .NET静态类构造函数 - C# ASP .NET static class constructor is called on every request 派生类的C#方法作为基础构造函数中的委托 - C# method from derived class as delegate in base constructor 如何使用C#从派生类中的构造函数中捕获异常? - How to catch exception from a constructor in a derived class with C#? 在C#中,如何从派生类构造函数访问派生类的属性? - In C# how do I access the properties of a derived class from the derived class constructor? C# static class 构造函数 - C# static class constructor 什么时候在C#中调用静态构造函数? - When is a static constructor called in C#? C#:如何从派生类的静态方法调用基类的静态方法? - C#: How do I call a static method of a base class from a static method of a derived class? 派生自另一个类且派生自抽象类的类的要包含在参数化构造函数中的c#字段 - c# Fields to include in parameterized constructor, of a class that has derived from another class that has derived from abstract class 为什么从 ServiceBase 继承的 C# 类跳过其派生类构造函数? - Why does a C# class which inherits from ServiceBase skip its derived class constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM