Yet another blog about WPF, Surface, SL, MVVM, NUI.... - [UPDATED] How to call the method from the base of the base of the current class ? (base.base.MyMethod) - Comments http://blog.lexique-du-net.com/index.php? In this blog we talk about WPF, Natural User Interface(NUI), Microsoft Surface, WIndows 7, Multitouch, Touchless, JavaFX, MVVM, patterns, tips, tricks .... and a lot of other things ! en Mon, 26 Feb 2024 15:40:43 +0100 Jonathan ANTOINE, All rights reserved http://blogs.law.harvard.edu/tech/rss Dotclear [UPDATED] How to call the method from the base of the base of the current class ? (base.base.MyMethod) - Jonathan ANTOINE http://blog.lexique-du-net.com/index.php?post/2011/05/11/How-to-call-the-method-from-the-base-of-the-base-of-the-current-class-%28base.base.MyMethod%29#c3030 urn:md5:453c0832f348b8ff12dc912038e3a432 Fri, 13 May 2011 10:25:44 +0200 Jonathan ANTOINE <p>@<a href="http://blog.lexique-du-net.com/index.php?post/2011/05/11/How-to-call-the-method-from-the-base-of-the-base-of-the-current-class-%28base.base.MyMethod%29#c3025" rel="nofollow">Thomas Levesque</a>&nbsp;:Hello Thomas,</p> <p>Thank you for your comment and it works like a charm <img src="themes/default/smilies/wink.png" alt=";-)" class="smiley" /> !</p> <p>Btw, there is still reflection in your solution, the only difference is that we use 'this' as the current instance without recreating one.</p> <p>I will update my post to add your solution <img src="themes/default/smilies/smile.png" alt=":-)" class="smiley" /></p> <p>Thx a lot !</p> [UPDATED] How to call the method from the base of the base of the current class ? (base.base.MyMethod) - Thomas Levesque http://blog.lexique-du-net.com/index.php?post/2011/05/11/How-to-call-the-method-from-the-base-of-the-base-of-the-current-class-%28base.base.MyMethod%29#c3025 urn:md5:305c99d1c08a3d64b04642e5989c0527 Fri, 13 May 2011 00:31:53 +0200 Thomas Levesque <p>Interesting...<br /> .<br /> Normally, you should never need to call base.base.SomeMethod, it's usually a sign of a bad design because you're not supposed to be aware of implementation details of the base class. But of course in the case of the TabControl you're not in control of the base classes, so it's different...<br /> .<br /> Anyway, I don't really like this solution... Two main reasons :<br /> - you have to create a dummy instance of the class, which is pretty bad for performance and could have unexpected side effects<br /> - it uses reflection (also bad for performance) on a non documented private field of the Delegate class. The implementation of this class could change in future versions, and this solution wouldn't work any more.<br /> .<br /> What you need to achieve this is a non-virtual call to GrandFather.MyMethod. This can be done by generating code at runtime with DynamicMethod:</p> <pre> class Child : Father { private delegate void MyMethodOpenDelegate(GrandFather instance, object param); private static readonly MyMethodOpenDelegate _grandFather_MyMethod; static Child() { DynamicMethod dm = new DynamicMethod( "grandFather_MyMethod", typeof(void), new Type[] {typeof(GrandFather), typeof(object)}, typeof(Child)); var il = dm.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_1); il.EmitCall(OpCodes.Call, typeof(GrandFather).GetMethod("MyMethod"), null); il.Emit(OpCodes.Ret); _grandFather_MyMethod = (MyMethodOpenDelegate)dm.CreateDelegate( typeof(MyMethodOpenDelegate)); } public override void MyMethod(object param) { _grandFather_MyMethod(this, param); } }</pre> <p>(note the use of OpCodes.Call instead of OpCodes.Callvirt)</p> [ping] [UPDATED] How to call the method from the base of the base of the current class ? (base.base.MyMethod) - DotNetKicks.com http://blog.lexique-du-net.com/index.php?post/2011/05/11/How-to-call-the-method-from-the-base-of-the-base-of-the-current-class-%28base.base.MyMethod%29#c3018 urn:md5:761147145e4258d01f6fd2d6605c32e1 Thu, 12 May 2011 00:00:34 +0200 DotNetKicks.com <p><a href="http://www.dotnetkicks.com/csharp/How_to_call_the_method_from_the_base_of_the_base_of_the_current_class">How to call the method from the base of the base of the</a></p> <!-- TB --> <p>You've been kicked (a good thing) - Trackback from DotNetKicks.com...</p> [ping] [UPDATED] How to call the method from the base of the base of the current class ? (base.base.MyMethod) - DotNetShoutout http://blog.lexique-du-net.com/index.php?post/2011/05/11/How-to-call-the-method-from-the-base-of-the-base-of-the-current-class-%28base.base.MyMethod%29#c3017 urn:md5:2f9a841379c0f7838a8e765a2175e57e Thu, 12 May 2011 00:00:22 +0200 DotNetShoutout <p><a href="http://dotnetshoutout.com/How-to-call-the-method-from-the-base-of-the-base-of-the-current-class-basebaseMyMethod">How to call the method from the base of the base of the</a></p> <!-- TB --> <p>Thank you for submitting this cool story - Trackback from DotNetShoutout...</p>