From: Duncan on 30 Jun 2010 06:19 Hi, I have a class 'A' that has a virtual member 'm()'. Another class 'B' inherits from A and overrides m. Yet another class 'C' inherits from B. A <- B <- C From C, can I call A's implementation of m(), or the member from the immediate inheritance from B? Thanks, -Duncan
From: Peter Duniho on 30 Jun 2010 11:21 Duncan wrote: > Hi, > > I have a class 'A' that has a virtual member 'm()'. Another class 'B' > inherits from A and overrides m. Yet another class 'C' inherits from B. > > A <- B <- C > > From C, can I call A's implementation of m(), or the member from the > immediate inheritance from B? Not using only language features, no. C# only exposes the immediate base class implementation, through "base". If you have control over B, you can add a way to have it call the base class member. Alternatively, you can of course always use reflection to access whatever type member you want. In general though, it would be ill-advised to do so. The language features exist in the way that they do because it leads to better, more robust, more maintainable code. Having C bypass the implementation of "m" in its own base class will significantly complicate the implementation and relationships of your classes, which can lead at a minimum to more work maintaining the code, if not actual hard-to-find and hard-to-fix bugs. Pete
From: Jeff Johnson on 30 Jun 2010 11:25 "Duncan" <a(a)b.com> wrote in message news:e3dcM3DGLHA.3732(a)TK2MSFTNGP02.phx.gbl... > I have a class 'A' that has a virtual member 'm()'. Another class 'B' > inherits from A and overrides m. Yet another class 'C' inherits from B. > > A <- B <- C > > From C, can I call A's implementation of m(), or the member from the > immediate inheritance from B? You can't call A's m() directly from C, no. You can call base.m() from class B and get A.m(), but there's no base.base.m() syntax you can call from C. You'd have to build some extra plumbing to allow this to happen. For example, in B you could have a BaseM() method which could simply call base.m(). Then in C you'd use BaseM() when you want A's implementation and just m() when you want B's. (You didn't mention that C overrode m() as well, so I'm assuming it doesn't.)
From: Duncan on 30 Jun 2010 12:47 Thanks for the info guys. There's an added complication with not having access to the A and B classes. So extending B to provide access to A isn't possible. I'll have a look at what I can do via reflection. Thanks, -Duncan
From: Jeff Johnson on 30 Jun 2010 13:35
"Duncan" <a(a)b.com> wrote in message news:%23dTA0PHGLHA.3640(a)TK2MSFTNGP02.phx.gbl... > I'll have a look at what I can do via reflection. That's easy: anything. |