c# 静态构造函数不是从派生类调用的

标签 c# inheritance static-constructor

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;
}

有什么想法可以使它工作,以便对 Buss 中的静态字段的引用触发 Bus 中的静态构造函数吗?

最佳答案

这对您很重要的事实可能意味着您错误地使用了静态构造函数。

考虑到这一点,您可以在 Bus 中创建一个静态构造函数,手动调用 Bus 中的静态构造函数。请注意,不可能多次运行静态构造函数。

关于c# 静态构造函数不是从派生类调用的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6390960/

相关文章:

c# - 在引用任何静态成员之前调用静态构造函数

c# - 系统.Data.EntitySqlException : The argument types 'Edm.String' and 'Edm.Int32' are incompatible for this operation

C++ 在 { 标记之前期望类名,继承错误

grails - Grails中多少个域类继承太多?

c# - 跟踪静态构造函数执行

c# - 确保调用基类的静态构造函数的最佳方法是什么?

c# - 覆盖 WinForms 中的选项卡行为

c# - 使用 Action<> 委托(delegate)时 int 递增的奇怪行为

c# - 方法未显示在 Swagger UI(带 Swashbuckle)中,但没有错误消息

Javascript 调用并应用构造函数