c# static constructor not called from derived class

China☆狼群 提交于 2019-12-04 07:36:47

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.

MSDN says that 'Static constructors are not inherited'. 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.

Calling Buss x = new Buss() will invoke the static constructor of 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.

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. 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.

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.

The example you provided only works because Buss is the type argument for itself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!