如何防止基构造函数被 C# 中的继承者调用? [英] How can I prevent a base constructor from being called by an inheritor in C#?
问题描述
我有一个(写得不好)基类,我想将它包装在代理对象中.基类类似于以下内容:
I've got a (poorly written) base class that I want to wrap in a proxy object. The base class resembles the following:
public class BaseClass : SomeOtherBase
{
public BaseClass() {}
public BaseClass(int someValue) {}
//...more code, not important here
}
而且,我的代理类似于:
and, my proxy resembles:
public BaseClassProxy : BaseClass
{
public BaseClassProxy(bool fakeOut){}
}
如果没有fakeOut"构造函数,预计会调用基构造函数.但是,有了它,我希望它不会被调用.无论哪种方式,我要么需要一种不调用任何基类构造函数的方法,要么需要某种其他方法来有效地代理这个(邪恶的)类.
Without the "fakeOut" constructor, the base constructor is expected to be called. However, with it, I expected it to not be called. Either way, I either need a way to not call any base class constructors, or some other way to effectively proxy this (evil) class.
推荐答案
如果没有在基类中显式调用任何构造函数,则无参数构造函数将被隐式调用.没有办法解决它,你不能在没有调用构造函数的情况下实例化一个类.
If you do not explicitly call any constructor in the base class, the parameterless constructor will be called implicitly. There's no way around it, you cannot instantiate a class without a constructor being called.
这篇关于如何防止基构造函数被 C# 中的继承者调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!