views:

799

answers:

6

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.

A: 
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();
Chris Lively
I can access base class method in the derived class, but I want to access from outside.
Ngu Soon Hui
In that case, there's something wrong with your design.
JasonTrue
A: 

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

Timothy Carter
Is it possible to cast MyDerived to MyBase and do the calling?I can't choose to not override DoMe() because both MyBase and MyDerived are not my code.
Ngu Soon Hui
If you have no control over MyDerived or MyBase, the only options I see are creating an instance of MyBase or creating your own class that Derives from MyBase and provides the functionality you want.
Timothy Carter
I wonder if its possible to do something like: base.base.DoMe()
Will
Lol, can't. And Skeet answered this four years ago.
Will
A: 

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.

Rob Kennedy
A: 

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.

Andrew Kennan
A: 

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.

Chris Lively
+1  A: 

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.

Justin Grant