Force calling the base method from outside a derived class

前端 未结 7 909
野性不改
野性不改 2020-12-10 18:56

I have two classes:

public class MyBase
{
    public virtual void DoMe()
    {

    }
}

public class MyDerived:MyBase
{
    public override void DoMe()
             


        
相关标签:
7条回答
  • 2020-12-10 19:11
    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();
    
    0 讨论(0)
  • 2020-12-10 19:22

    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.

    0 讨论(0)
  • 2020-12-10 19:26

    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.

    0 讨论(0)
  • 2020-12-10 19:27

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

    0 讨论(0)
  • 2020-12-10 19:27

    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();
    
    0 讨论(0)
  • 2020-12-10 19:33

    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.

    0 讨论(0)
提交回复
热议问题