我如何防止在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
}
和,我的代理类似于:
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屋!