Multiple level base calls

  • Thread starter Thread starter lucas_denoir
  • Start date Start date
L

lucas_denoir

I'm having some serious trouble accessing a virtual method of a base
class - that is not the immidate base class.

This is the basic situation that I have:

===========================================================
class A
{
public virtual string PrintMe()
{
return "Original A";
}
}

class B : A
{
public override string PrintMe()
{
return "Overriden B";
}
}

class C : B
{

public override string PrintMe()
{
return "Overriden C";
}

}
===========================================================

Now, what I'm trying to do is to from an instance of C call A's
PrintMe() method. I've tried various delegate twists, I've tried
reflection - without any luck. It insists on returning the top of the
virtual call table - C's PrintMe().

Any ideas or suggestions?

thanks
--Lucas
 
Why do you want to do this? Maybe there is another way to get the
effect you want, without having to call up the class hierarchy like
that.
 
Basically it's about deploying components on a selectable level. The
whole story is a bit complicated, but basically the story is that each
class has its own to/from XML methods that store various properties and
settings that belong to the class.

The basic idea is that there are base classes, which are .NET Compact
framework compatible while the user works with classes that inherit
them and have extensive GUI etc. When the system is deployed, we need
to get the XML of the base classes..

Anyway, the whole thing is too complicated to briefly cover here -
there's a bunch of restrictions on how it can be done. Bottom line is
that I really need to do it the way I described it.
 
How about using base.PrintMe from B? Have a protected method in B that
calls base.PrintMe() and call that method from C.

using System;

class A
{
public virtual void PrintMe() { Console.WriteLine("A"); }
}

class B : A
{
public override void PrintMe() { Console.WriteLine("B"); }
protected void BasePrintMe() { base.PrintMe();}
}

class C : B
{
public override void PrintMe() { BasePrintMe(); }

}

class T
{
public static void Main()
{
A a = new C();
a.PrintMe();
}
}

Regards
Senthil
 
Lucas... The language does support:
1) Use new instead of override which destroys polymorphic behaviour but
allows access to the base method through a reference variable of the
base type.
2) In the subclass method you can call super.PrintMe()
class Class2 : Class1
{
public override string Test()
{
return base.Test()+"Class2";
}
}

Regards,
Jeff
Now, what I'm trying to do is to from an instance of C call A's
PrintMe() method. I've tried various delegate twists, I've tried
reflection - without any luck. It insists on returning the top of the
virtual call table - C's PrintMe().<
 
Basically it's about deploying components on a selectable level. The
whole story is a bit complicated, but basically the story is that each
class has its own to/from XML methods that store various properties and
settings that belong to the class.

The basic idea is that there are base classes, which are .NET Compact
framework compatible while the user works with classes that inherit
them and have extensive GUI etc. When the system is deployed, we need
to get the XML of the base classes..

Anyway, the whole thing is too complicated to briefly cover here -
there's a bunch of restrictions on how it can be done. Bottom line is
that I really need to do it the way I described it.

Well, the bottom line is that you *can't* do what you originally asked.
You can't call a grandparent method if it's overridden in the parent.

However, if you make each method call the base method, making them pick
and choose what they react to, it should work fine.
 
Unfortunately that's not an option as I don't know beforehand how long
the chain is. I need to do this with an arbitrary amount of inheretence
steps.
 
Well, the bottom line is that you *can't* do what you originally
asked.
You can't call a grandparent method if it's overridden in the parent.

Well, that's not quite true either. I have no problem doing it through
IL, so as far as the runtime environment goes, it's no problem. It's c#
that's blocking me.
 
Thanks, but I'm aware of the basic language limitations. Unfortunately,
as I said in the original post, I can't use new. And base doesn't cut
it as it only goes one level and I can't get to it from outside.

What I'm trying to do is get around it through reflection, but without
resorting to have to emit in IL what I need. I don't see why the
runtime constantly refuses me access to any level of the virtual call
table. I always end up at the top, with the last overriden class. The
furthest I have come is to get an actual function pointer (IntPtr) to
the method I want, but when I use it through
RuntimeMethodHandle->MethodHandle, I again end up at the top of the
virtual table, with a call to the last method in the virtual table.

--Lucas
 
Thanks, but I'm aware of the basic language limitations. Unfortunately,
as I said in the original post, I can't use new. And base doesn't cut
it as it only goes one level and I can't get to it from outside.

What I'm trying to do is get around it through reflection, but without
resorting to have to emit in IL what I need. I don't see why the
runtime constantly refuses me access to any level of the virtual call
table. I always end up at the top, with the last overriden class. The
furthest I have come is to get an actual function pointer (IntPtr) to
the method I want, but when I use it through
RuntimeMethodHandle->MethodHandle, I again end up at the top of the
virtual table, with a call to the last method in the virtual table.

--Lucas

it's restricted because that's just how polymorphism suppose to work.

and why can't you use new? from what you described, sounds to me like you
want to hide the base method, not override it.
 
Just because you can, doesn't mean you should.

Well, that's not quite true either. I have no problem doing it through
IL, so as far as the runtime environment goes, it's no problem. It's c#
that's blocking me.
 
I can't use new, because I'm not in control of writing the children
clasess, and forgetting to use new would have nasty consequences.

To explain a bit more, suppose you have these classes:

Atom - contains basic from/to XML serialization functions
Adder:Atom - a class that adds two numbers
AdderGui: Adder - graphical version of Adder, in windows forms
AdderNiceGui: AdderGui, with surround sound, 3d blah blah - i.e a whole
bunch of things

Each class sets/gets XML to store its current state. This is done by
overriding the ToXml and FromXml methods, that have originally been
defined in Atom. The XML works by iteratively encapsulating the lower
levels. So for instance, the Adder XML might look like this (produced
by it's ToXml() ):

<adder class=Adder>
<anum>1</anum>
<bnum>2</bnum>
</adder>

The AdderGui might then look like this:

<addergui class=AdderGui>
<window size=100></>
<base class=Adder>
<adder class=Adder>
<anum>1</anum>
<bnum>2</bnum>
</adder>
</base>
</adderGui>

The AdderNiceGui might look something like this:

<addernicegui class=AdderNiceGui>
<sound mode=surround></sound>
<gfx mode=d3d></gfx>
<base class=AdderGui>
<addergui class=AdderGui>
<window size=100></>
<base class=Adder>
<adder class=Adder>
<anum>1</anum>
<bnum>2</bnum>
</adder>
</base>
</adderGui>
</base>
</addernicegui>

Ok?

Now the user will during run-time use the AdderNiceGui and AdderGui
classes. Once he is done configuring them, he wants to deploy them to
..NET compact. Only the pure Adder class is compact framework
compatible, so I need from the AdderNiceGui class, extract the Adder
class, for deployment. No problems there. Now we want the XML for just
the Adder class - which we get from Adder's ToXml(). And this is where
the problem comes in that from AdderNiceGui, there seems to be no
(civilized) way of getting through a call to Adder's ToXml.

There are a number of design restrictions - such as easy implementation
of new classes as well as that the individual class XML can be whatever
(in short, you can't trust to extract any structural info from the
XML). Each class is responsible for getting the XML from its baseclass
and incorporating it to its XML etc

That's the practical context of it.
 
Well, that's not quite true either. I have no problem doing it through
IL, so as far as the runtime environment goes, it's no problem. It's c#
that's blocking me.

You can't do it in C#. Your question is in a C# newsgroup, so I think
it's reasonable to assume you wanted a C# solution.

(There's the matter of "should" as well, where I'd say that you
shouldn't do this even if you could.)
 
Thanks, but I'm aware of the basic language limitations. Unfortunately,
as I said in the original post, I can't use new. And base doesn't cut
it as it only goes one level and I can't get to it from outside.

Base only goes one level, but if every method calls base, then you end
up getting all the way up the tree.
 
It sounds like you just want to use classes that are already in (and named
different names) - from your base classes. Is this correct?

If so, you just call them - the runtime "figured out" that you mean call it
from a base class if another version doesn't exist higher up..

In other words, if you have BaseTest.FormatIt - then you inherit from that
and create BaseTestTwo.FormatItBetter and you inherit from BaseTestTwo and
create BaseTestThree and and have FormatItBest..

From BaseTestThree, you have access to all of those methods:

BaseTestThree objBTT = new BaseTestThree();
objBTT.FormatIt();
objBTT.FormatItBetter();
objBTT.FormatItBest();

Those are all valid. Is that what you are trying to do??
 
Rather than doing tricky things with Reflection and IL, why not just do
this:

Adder declares not two, but _four_ public methods:

public virtual ... FromXml(...) {...}

public virtual ... ToXml(...) {...}

which is what you have (and have overridden in the various child
classes) and

public ... FromCompactCompatibleXml(...)
{
return FromXml(...);
}

public ... ToCompactCompatibleXml(...)
{
return ToXml(...);
}

which emits XML that is compatible with the Compact Framework version
of the class, and is not virtual, so cannot be overridden.

Child classes override and embellish ToXml and FromXml, but when it's
time to deploy to the Compact Framework, you call the CompactCompatible
methods, which are inherited directly from the base class.

In effect, you give alternate names to FromXml() and ToXml() in the
base class, and forbid inheriting classes from overriding the meaning
of those names, so that you can always get at them from any child class.
 
In effect, you give alternate names to FromXml() and ToXml() in the
base class, and forbid inheriting classes from overriding the meaning
of those names, so that you can always get at them from any child
class.

I can't quite do that as the level at which you choose to deploy is
arbitrary. There can be several components (classes) in the same line
of inheritence that need to be deployed.

And in addition, it isn't an aesthetically pleasing solution, as the
XML method would have to be implemented twice in the deployable class.
Emiting IL is more complicated, but this I can do on my side, sparing
those that write the components to write redundant code.
 
Base only goes one level, but if every method calls base, then you end
up getting all the way up the tree.

Nah, base is hard-wired at compile time to the actuall base class where
it is defined (not too strange). For it to work, each child-class would
have to actually implement the base call, resulting in unnecessary and
redundant code that could easily introduce errors throughout the entire
inheritance chain.

Thanks for your suggestions, they are appreciated, but I'm really not
looking for a workaround to the problem. What I need help with is how
to accomplish it, through reflection, without resorting to IL.
 
Those are all valid. Is that what you are trying to do??

Nope, can't do that either. The only thing I know about the classes
comes from a simple interface (that specifies To/From Xml methods).
Their assemblies are dynamically loaded (plugins) and I have no
knowledge of them at compile time - apart from that they implement a
number of interfaces.
 
You can't do it in C#. Your question is in a C# newsgroup, so I think
it's reasonable to assume you wanted a C# solution.

Fair point, I probably should have posted it in one of the more
specific .NET framework groups, since it's more about reflection than
about C#. The problem is that this quite doable from for instance
managed C++, so my problem is C# specific.
 
Back
Top