<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Invoking a virtual method non-virtually</title><link>http://blogs.msdn.com/rmbyers/archive/2008/08/16/invoking-a-virtual-method-non-virtually.aspx</link><description>Method calls using the C# ‘ base ’ keyword get compiled to an IL ‘ call ’ instruction, rather than the ‘ callvirt ’ that is normally used . This is the one case in C# where a virtual method can be invoked without virtual dispatch. The CLR allows it to</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>funny wallpaper &amp;raquo; Invoking a virtual method non-virtually</title><link>http://blogs.msdn.com/rmbyers/archive/2008/08/16/invoking-a-virtual-method-non-virtually.aspx#8872755</link><pubDate>Sun, 17 Aug 2008 02:32:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8872755</guid><dc:creator>funny wallpaper &amp;raquo; Invoking a virtual method non-virtually</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://housesfunnywallpaper.cn/?p=1067"&gt;http://housesfunnywallpaper.cn/?p=1067&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Invoking a virtual method non-virtually</title><link>http://blogs.msdn.com/rmbyers/archive/2008/08/16/invoking-a-virtual-method-non-virtually.aspx#8890124</link><pubDate>Sat, 23 Aug 2008 12:07:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8890124</guid><dc:creator>atykhyy</dc:creator><description>&lt;p&gt;I was investigating non-virtual calls the other day, because DynamicILGenerator overrides some ILGenerator's methods with functions throwing NotSupportedExceptions for no reason I could discover and I had to call them. At first I developed a solution with dynamic methods, very similar to what you propose here, but actually there *is* a way do do it with delegates. The relevant code looks like this:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;private delegate void BeginFaultBlock &amp;nbsp;() ;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;private readonly static ConstructorInfo dBeginFaultBlock &amp;nbsp;= typeof (BeginFilterBlock).GetConstructors () [0] ;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;private readonly static IntPtr &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;iBeginFaultBlock &amp;nbsp;= typeof (ILGenerator).GetMethod (&amp;quot;BeginFaultBlock&amp;quot;) &amp;nbsp; &amp;nbsp; &amp;nbsp; .MethodHandle.GetFunctionPointer () ;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static BeginFaultBlock MakeBeginFaultBlockDelegate (ILGenerator generator)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (BeginFaultBlock) dBeginFaultBlock.Invoke (new object[] { generator, iBeginFaultBlock &amp;nbsp;})) () ;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;I couldn't get invocation through delegates to work reliably when the delegate receives the generator as an argument, i.e.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;private delegate void BeginFaultBlock (ILGenerator generator) ; // no good&lt;/p&gt;
&lt;p&gt;but maybe UnboundDelegate can handle this.&lt;/p&gt;</description></item><item><title>re: Invoking a virtual method non-virtually</title><link>http://blogs.msdn.com/rmbyers/archive/2008/08/16/invoking-a-virtual-method-non-virtually.aspx#8896220</link><pubDate>Tue, 26 Aug 2008 08:40:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8896220</guid><dc:creator>rmbyers</dc:creator><description>&lt;p&gt;Interesting. &amp;nbsp;I didn't realize you could create a delegate with an explicit method address like that - thanks! &amp;nbsp;I'm not sure if there are any gotchas associated with the use of RuntimeMethodHandle.GetFunctionPointer, etc.&lt;/p&gt;
&lt;p&gt;Of course, using a trick like this to get around the fact that DynamicILGenerator doesn't support some opporations is almost certainly asking for trouble (at the very least, it might break in a future version or service pack of the runtime, since it's not verifiable to make a call circumventing virtual dispatch like this).&lt;/p&gt;
</description></item></channel></rss>