|
tech
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to call a method defined in grandparent class?I'll need something like:
base.base.Show(); However, this doesn't work. The grandparent class name is "BaseForm". What's the correct syntax to call the "Show" method defined in the "BaseForm" class? > base.base.Show(); Well if you can send this ref to a function and the function can cast past reference to any base type say Object like show in MSDN - Textbox Class Page... System.Object System.MarshalByRefObject System.ComponentModel.Component System.Windows.Forms.Control System.Windows.Forms.TextBoxBase System.Windows.Forms.TextBox So Here TextBox is like your derived class so lets access TextBox's grandparent... TextBox.Text proptery is defined in base class Control so calling... .... //in some function... TextBox TextBoxObj = new TextBox(); ... // Setup other TextBoxObj members and add to parent control's children collection... TextBoxObj.Text = "Something..."; .... [or we can do the same thing with this...] .... ... ((Control)TextBoxObj).Text = "Something..."; .... This works cause you cast TextBoxObj ref to a Control ref and access Text thur Control ref... So now to access your derived grandparent base class you can use... .... //in some function... //base.base.Show(); ((GRANDPARENTBASECLASSHERE)TextBoxObj).Show(); .... Also there is nothing wrong with just calling Show() alone unless virtual overrides or name ambiguity get in the way like... .... //base.base.Show(); Show(); .... Hope that helps... On Nov 26, 6:58 am, "bj7lewis" <bj7le***@rio.com> wrote: <snip>> So now to access your derived grandparent base class you can use... No, that won't do it, because overriding is done at runtime, not> > ... > //in some function... > //base.base.Show(); > ((GRANDPARENTBASECLASSHERE)TextBoxObj).Show(); compile time. If a method is overridden in a child class, the grandchild cannot access the original behaviour. Jon Jon Skeet [C# MVP] wrote:
Show quote > On Nov 26, 6:58 am, "bj7lewis" <bj7le***@rio.com> wrote: Which, IMO, is a good thing. Otherwise, the contract that the > > <snip> > >> So now to access your derived grandparent base class you can use... >> >> ... >> //in some function... >> //base.base.Show(); >> ((GRANDPARENTBASECLASSHERE)TextBoxObj).Show(); > > No, that won't do it, because overriding is done at runtime, not > compile time. If a method is overridden in a child class, the > grandchild cannot access the original behaviour. > > Jon child-class guarantees with its implementation goes out the window if a descendant could skip its code entirely. On Nov 26, 1:03 pm, Lasse Vågsæther Karlsen <la***@vkarlsen.no> wrote:
<snip> > > No, that won't do it, because overriding is done at runtime, not Absolutely - it's part of encapsulation, effectively.> > compile time. If a method is overridden in a child class, the > > grandchild cannot access the original behaviour. > > Which, IMO, is a good thing. Otherwise, the contract that the > child-class guarantees with its implementation goes out the window if a > descendant could skip its code entirely. Jon Curious wrote:
> I'll need something like: You can't. And you shouldn't. But the IL hacker in me can't resist > > base.base.Show(); > > However, this doesn't work. The grandparent class name is "BaseForm". > What's the correct syntax to call the "Show" method defined in the > "BaseForm" class? pointing out that you can! Basically, the idea is to make a non-virtual call to a virtual method (eugh!). I've included some sample code, but be warned that I've not tested it and it's late here. Every time you use this code, an OO purist kills a puppy. delegate void ShowDelegate(); public void GrandParentShow() { DynamicMethod show = new DynamicMethod( "_Show", null, new Type[0], typeof(Program).Module); //This is just a reference to // your module. You don't have to use typeof(Program) ILGenerator il = show.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); //You could get the current type and call //currentType.Parent.Parent instead of //hardcoding the grandparent il.Emit(OpCodes.Call, typeof(BaseForm).GetMethod("Show")); il.Emit(OpCodes.Ret); ShowDelegate del = (ShowDelegate)show.CreateDelegate(typeof(ShowDelegate)); del(); } Alun Harford |
|||||||||||||||||||||||