Force calling the base method from outside a derived class

这一生的挚爱 提交于 2019-12-17 19:59:41

问题


I have two classes:

public class MyBase
{
    public virtual void DoMe()
    {

    }
}

public class MyDerived:MyBase
{
    public override void DoMe()
    {
        throw  new NotImplementedException();
    }
}

And I have the following code to instantiate MyDerived:

        MyDerived myDerived=new MyDerived();

The thing is how to call DoMe of the base class? If I use myDerived.DoMe(), then the derived method wil be called, resulting in an exception. I tried to cast myDerived to MyBase, yet it is still the derived version of the method that gets called.

Edit: As mentioned in the below comment, I can't change eitehr MyDerived or MyBase because they are not my code.


回答1:


There's a solution, but it's ugly: use reflection to get the base-class method, and then emit the IL necessary to call it. Check out this blog post which illustrates how to do this. I've successfully used this approach it to call the base class's implementation of a method when all I have is a reference to a derived class which overrides that method.




回答2:


You can't call the base-class version.

If the method doesn't work on the derived class, then it's rather unlikely the base version of the method will work when called on an instance of the derived class. That's just asking for trouble. The class was not designed to work that way, and what you're trying to do will probably just cause other parts of the class to behave unpredictably.

Why do you need to call this method on the object when the object tells you outright that it won't work?

It seems to me that these classes have some design flaws, and if you're not allowed to change the classes, maybe you're allowed to change to a more well designed library instead.




回答3:


To call MyBase.DoMe() from an external class you would need either an instance of MyBase or a derived instance that does not override DoMe(). A method declared as a virtual will be called on the actual runtime type of the object, not the type of the object, which is why casting to MyBase does not change what method is called. If however the method was not declared in MyBase as virtual and MyDerived still implemented DoMe() it would be "hiding" the MyBase's implementation. Therefore, if the reference was MyDerived it would call MyDerived.DoMe(), but in this case casting to MyBase myBase = (MyBase)myDerived and then calling myBase.DoMe() would call MyBase.DoMe().




回答4:


The derived class does not need to provide an implementation of the method. Remove it and the implementation in the base class will be called by default.

If, unlike your example, the method in the base class is abstract and you must provide an implementation but it doesn't make sense for the derived class to provide one then there is probably something wrong with the design of the classes.




回答5:


public class MyDerived:MyBase{    
    public override void DoMe()    
    {        
        base.DoMe();
    }
}

EDIT:

You can't access the base classes method from the "outside" without going through the subclasses method. Your only option is to instantiate your base class directly and call it's method.

MyBase mb = new MyBase();
mb.DoMe();



回答6:


Given your restrictions, another possibility exists:

Download .Net Reflector. Decompile the existing code then make any changes you need to support your situation.

Of course, review the legality of this before continuing.




回答7:


This question is so old but I don't see the following option:

You can use the 'new' keyword to specify overlapping methods. Then you would simply cast to the class whose method you wish to call.

    public class MyBase
    {
        public virtual void DoMe()
        {

        }
    }

    public class MyDerived:MyBase
    {
//note the use of 'new' and not 'override'
        public new void DoMe()
        {
            throw  new NotImplementedException();
        }
    }

Implementation

var myDerived = new MyDerived();
var derivedDoMe = myDerived.DoMe();
var baseDoMe = ((MyBase)myDerived).DoMe();


来源:https://stackoverflow.com/questions/437926/force-calling-the-base-method-from-outside-a-derived-class

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