Reputation: 12745
I can invoke the parent class method by using base.virtualParentMethod(). But how do I call the method in parent-parent class without creating an object of it, in the following scenario.
class A
{
public virtual void virtualParentMethod()
{
Console.WriteLine("A");
}
}
class B : A
{
public override void virtualParentMethod()
{
Console.WriteLine("B");
}
}
class C : B
{
public override void virtualParentMethod()
{
//base.virtualParentMethod();
//This is where I want to invoke the method of A
//So that out Will be : A
}
}
Upvotes: 1
Views: 16643
Reputation: 6527
You've already created an instance of A, by creating an instance of C (as C : B, B : A), so there is a way you can do this with a small modification. Now, without indicating whether or not this is a good idea or not;
If you use the 'new' keyword instead of override on your derived method implementations, then you can call the base implementation of a method directly, bypassing the intermediate class implementation, by casting the object to the type who's implementation you want to use, like so;
class A
{
public virtual void virtualParentMethod()
{
Console.WriteLine("A");
}
}
class B : A
{
public new void virtualParentMethod()
{
Console.WriteLine("B");
}
}
class C : B
{
public new void virtualParentMethod()
{
// casting this to A will allow you to call the base class implementation
((A)this).virtualParentMethod();
}
}
Note that if you make this change, you've introduced a behavioural change to any callers of the method, depending on how they refer to the object. And if you try to cast in this way in the existing C.virtualParentMethod implementation (declared 'override'), you are really just calling the method itself and will get in an infinite loop.
Or, you could just reconsider your class design. :-)
Upvotes: 0
Reputation: 236328
If you need some parent functionality in not direct children of parent, then you should move that functionality to separate method:
class A
{
public virtual void VirtualParentMethod()
{
Foo();
}
protected void Foo()
{
Console.WriteLine("A");
}
}
class B : A
{
public override void VirtualParentMethod()
{
Console.WriteLine("B");
}
}
class C : B
{
public override void VirtualParentMethod()
{
Foo();
}
}
UPDATE
Also consider:
C
directly from A
B
from C
Upvotes: 4
Reputation: 64527
You cannot pick what version of a derived method to run using the base
syntax. The only way to run a specific method would be to instantiate a version of A
inside C
that can be used for that method (basically, a real instance of A
).
However I personally wouldn't do this. This is indicative of a design issue with your inheritance chain.
If you need to ensure that C
runs A
, why not inherit A
from C
- C : A
.
Upvotes: 2
Reputation: 21752
You can't. using base.MethodName will always call the closest implementation up the hierachy and there's no way you can by pass this without explicitly declaring a method that can be called
it's also worth noting that if you find yourself in a situation where you wish to do this odds are you have a design flaw.
Upvotes: 0
Reputation: 62265
You simply can not, cause that is an essence of Virtual Method Table, so virtual
keyword and its override. Invokation method's address will be lookuped from corresponding real type Virtual Methods Table.
Upvotes: 0