[英]Why is my inherited class not hiding my base class method?
I have a project which uses Autofac to instantiate the objects 我有一个使用Autofac实例化对象的项目
builder.RegisterType<AbcWebWorkContext>().As<IWorkContext>().InstancePerHttpRequest();
This AbcWebWorkContext
is a subclass of WebWorkContext
: 这
AbcWebWorkContext
是的子类WebWorkContext
:
public partial class AbcWebWorkContext : WebWorkContext
In my AbcWebWorkContext
I would like to hide a method and a property from the parent class 在我的
AbcWebWorkContext
我想从父类中隐藏方法和属性
protected new Customer GetCurrentCustomer(){ //do stuff }
new public Customer CurrentCustomer { //do studd }
But when someone calls 但是当有人打电话
_workContext.CurrentCustomer
The base class property is called. 基类属性被调用。 If I call it like this
如果我这样称呼它
((AbcWebWorkContext) _workContext).CurrentCustomer
it works. 有用。
I would like to know why I am not able to hide the parent method. 我想知道为什么我无法隐藏父方法。 I can't change the called class because it is in
NopCommerce
's core, which I would not like to change. 我不能更改被调用的类,因为它是
NopCommerce
的核心,因此我不想更改。
Why is it not hiding the method? 为什么不隐藏该方法?
Base class declaration of the methods: 方法的基类声明:
protected Customer GetCurrentCustomer() { // do stuff }
public Customer CurrentCustomer{ // do stuff }
calling GetType()
on _workcontext
will output 在
_workcontext
上调用GetType()
将输出
{Name = "AbcWebWorkContext" FullName = "Nop.Web.Framework.AbcWebWorkContext"}
The type hierarchy is IWorkContext
(interface) « WebWorkContext
« AbcWebWorkContext
类型层次结构是
IWorkContext
(接口)« WebWorkContext
« AbcWebWorkContext
_workContext
is declared as IWorkContext
and Autofac generates an instance as AbcWebWorkContext
(as shown above) _workContext
声明为IWorkContext
,Autofac生成一个实例作为AbcWebWorkContext
(如上所示)
The new
keyword means that the subclass's method hides the baseclass's CurrentCustomer
instead of overriding, so WebWorkContext.CurrentCustomer
is a completely different method than AbcWebWorkContext.CurrentCustomer
. new
关键字意味着子类的方法将隐藏基类的CurrentCustomer
而不是覆盖它,因此WebWorkContext.CurrentCustomer
是与AbcWebWorkContext.CurrentCustomer
完全不同的方法。
You must declare the base class's method as 您必须将基类的方法声明为
virtual Customer CurrentCustomer { ... }
And the subclass's method 和子类的方法
override Customer CurrentCustomer { ... }
I suggest you read more about polymorphism in c# . 我建议您阅读有关c#中多态性的更多信息。
If the methods you're using are actually defined in an interface, IWebWorkContext
, you simply encapsulate the base class rather than inheriting from it, like this: 如果您使用的方法实际上是在接口
IWebWorkContext
定义的, IWebWorkContext
只需封装基类,而不是从基类继承即可,如下所示:
class AbcWebWorkContext : IWebWorkContext
{
private WebWorkerContext _inner = new WebWorkerContext();
public Customer CurrentCustomer { ... }
}
If you can't make the base class's method virtual, you have to do something much less elegant, like reflection: 如果不能使基类的方法虚拟化,则必须做一些不太优雅的事情,例如反射:
var prop = _workContext.GetType().GetProperty("CurrentCustomer");
var value = (Customer)prop.GetValue(_workContext, new object[0]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.