C#静态构造函数不是从派生类叫 [英] c# static constructor not called from derived class

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

问题描述

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?

推荐答案

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

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. Note that it's not possible to run a static constructor more than once.

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

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