<?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>Small place on Earth</title><link>http://blogs.msdn.com/b/alexghi/</link><description>&amp;quot;Imagination is more important than knowledge&amp;quot; - Albert Einstein</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>Visual Basic hides virtual methods from C#? Not really! </title><link>http://blogs.msdn.com/b/alexghi/archive/2010/01/27/visual-basic-hides-virtual-methods-from-c-not-really.aspx</link><pubDate>Wed, 27 Jan 2010 23:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9954395</guid><dc:creator>Alex Ghiondea - MSFT</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/alexghi/rsscomments.aspx?WeblogPostID=9954395</wfw:commentRss><comments>http://blogs.msdn.com/b/alexghi/archive/2010/01/27/visual-basic-hides-virtual-methods-from-c-not-really.aspx#comments</comments><description>&lt;P&gt;I was recently asked to investigate a&amp;nbsp;potential&amp;nbsp;issue about&amp;nbsp;the C# to Visual Basic language inter-op. The punch line was that C# could not call a method defined in a library written in Visual Basic. However, if you used C# or Visual Basic for both assemblies, everything worked as expected.&lt;/P&gt;
&lt;P&gt;The Visual Basic code used to produce the library is:&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  1: &lt;SPAN style="COLOR: #0000ff"&gt;Public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;MustInherit&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Class&lt;/SPAN&gt; Base
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  2:     &lt;SPAN style="COLOR: #0000ff"&gt;Public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Overridable&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Sub&lt;/SPAN&gt; Print()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  3:     &lt;SPAN style="COLOR: #0000ff"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Sub&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  4: 
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  5:     &lt;SPAN style="COLOR: #0000ff"&gt;Public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Overridable&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Sub&lt;/SPAN&gt; Print(&lt;SPAN style="COLOR: #0000ff"&gt;ByVal&lt;/SPAN&gt; preview &lt;SPAN style="COLOR: #0000ff"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Boolean&lt;/SPAN&gt;)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  6:     &lt;SPAN style="COLOR: #0000ff"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Sub&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  7: &lt;SPAN style="COLOR: #0000ff"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Class&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  8: 
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  9: 
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt; 10: &lt;SPAN style="COLOR: #0000ff"&gt;Public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;MustInherit&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Class&lt;/SPAN&gt; Derived
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt; 11:     &lt;SPAN style="COLOR: #0000ff"&gt;Inherits&lt;/SPAN&gt; Base
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt; 12: 
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt; 13:     &lt;SPAN style="COLOR: #0000ff"&gt;Public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Overrides&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Sub&lt;/SPAN&gt; Print()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt; 14:     &lt;SPAN style="COLOR: #0000ff"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Sub&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt; 15: &lt;SPAN style="COLOR: #0000ff"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Class&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;For those who are not proficient in Visual Basic the code means that Base is an abstract class (MustInherit) with two virtual (Overridable) methods called Print. The Derived class is also abstract and derives from Base. If defines a method that overrides the method Print (the one without any parameters).&lt;/P&gt;
&lt;P&gt;Here is the C# code that references the library:&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  1: &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; MyClass : VBLibrary.Derived
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  2: {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  3:     &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; Foo()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  4:     {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  5:         Print(&lt;SPAN style="COLOR: #0000ff"&gt;true&lt;/SPAN&gt;);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  6:     }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  7: }&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;When you compile the C# code referencing the Visual Basic library you get this error: &lt;/P&gt;
&lt;P&gt;&lt;FONT color=#ff0000&gt;CS1501: No overload for method 'Print' takes 1 arguments&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Inspecting the VB library with ILDasm shows that both Print methods are emitted by the VB compiler. Since the methods are emitted in the library and when using C# to write the library the code compiles, let’s see if there is a difference between the code generated by the VB compiler and the one generated by the C# compiler.&lt;/P&gt;
&lt;P&gt;The code generated by the Visual Basic compiler for the Derived.Print() method is this:&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  1:  .&lt;SPAN style="COLOR: #0000ff"&gt;method&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; virtual &lt;SPAN style="COLOR: #0000ff"&gt;instance&lt;/SPAN&gt; void Print() &lt;SPAN style="COLOR: #0000ff"&gt;cil&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;managed&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  2:     {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  3:     }
&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;while the code generated by the C# compiler is&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  1:  .&lt;SPAN style="COLOR: #0000ff"&gt;method&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;hidebysig&lt;/SPAN&gt; virtual &lt;SPAN style="COLOR: #0000ff"&gt;instance&lt;/SPAN&gt; void Print() &lt;SPAN style="COLOR: #0000ff"&gt;cil&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;managed&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  2:     {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  3:     }
&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;Notice the extra &lt;STRONG&gt;hidebysig&lt;/STRONG&gt; modifier on the method generated by the C# compiler?&lt;/P&gt;
&lt;P&gt;According to the CLR ECMA Specifications (Section 15.4.2.2.) hidebysig - "specifies that the declared method hides all methods of the base class types that have a matching method signature; when omitted, the method should hide all methods of the same name, regardless of the signature." In C# and VB the signature of a method consists of the method name and its parameter types.&lt;/P&gt;
&lt;P&gt;This is the root cause of the problem. &lt;/P&gt;
&lt;P&gt;When the C# compiler tries to bind the Print(true) call it first has to do a member lookup for the method Print. The simplified process steps are (read Section 7.3 of the C# spec for more info):&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Determine the list of all the methods named Print defined by MyClass and its base classes. While building this list, exclude all the methods that are marked override &lt;/LI&gt;
&lt;LI&gt;Remove all the members that are hidden by other members &lt;/LI&gt;
&lt;LI&gt;Return the list &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Because the referenced library does not mark the Print() method as hidebysig the method will &lt;STRONG&gt;hide all the methods named Print&lt;/STRONG&gt; defined on the Base class. And according to the member lookup algorithm they have to be excluded from the candidate set (step 2). This explains the error that the C# compiler gives.&lt;/P&gt;
&lt;P&gt;So the behavior is by design. If you do want to be able to call both Print methods from C#, you have to write the VB code like this:&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  1: &lt;SPAN style="COLOR: #0000ff"&gt;Public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;MustInherit&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Class&lt;/SPAN&gt; Derived
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  2:     &lt;SPAN style="COLOR: #0000ff"&gt;Inherits&lt;/SPAN&gt; Base
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  3: 
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  4:     &lt;SPAN style="COLOR: #0000ff"&gt;Public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Overloads&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Overrides&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Sub&lt;/SPAN&gt; Print()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  5:     &lt;SPAN style="COLOR: #0000ff"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Sub&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  6: &lt;SPAN style="COLOR: #0000ff"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;Class&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;By specifying both modifiers (&lt;STRONG&gt;Overloads Overrides&lt;/STRONG&gt;) on the Print method you tell the VB compiler that the method Print overrides the base method but has to be considered as an overload to the rest of the Print methods defined on the base class.&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9954395" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/code/">code</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/Visual+Basic/">Visual Basic</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/C_2300_/">C#</category></item><item><title>Using the static type of a variable in a dynamic call</title><link>http://blogs.msdn.com/b/alexghi/archive/2010/01/14/using-the-static-type-of-a-variable-in-a-dynamic-call.aspx</link><pubDate>Thu, 14 Jan 2010 00:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9948101</guid><dc:creator>Alex Ghiondea - MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/alexghi/rsscomments.aspx?WeblogPostID=9948101</wfw:commentRss><comments>http://blogs.msdn.com/b/alexghi/archive/2010/01/14/using-the-static-type-of-a-variable-in-a-dynamic-call.aspx#comments</comments><description>&lt;P&gt;As I said &lt;A href="http://blogs.msdn.com/alexghi/archive/2010/01/04/when-does-the-compiler-dispatch-a-call-dynamically.aspx" mce_href="http://blogs.msdn.com/alexghi/archive/2010/01/04/when-does-the-compiler-dispatch-a-call-dynamically.aspx"&gt;before&lt;/A&gt;, figuring out when the compiler will dispatch a call dynamically is simple: anytime the receiver or one of the arguments of a call is dynamic. And when using a variable of type dynamic means that the Runtime Binder will use the runtime type of the value stored in that variable and NOT it’s compile-time type. You can read more about compile time versus runtime type in my &lt;A href="http://blogs.msdn.com/alexghi/archive/2009/05/20/compile-time-type-vs-runtime-type-of-a-variable.aspx" mce_href="http://blogs.msdn.com/alexghi/archive/2009/05/20/compile-time-type-vs-runtime-type-of-a-variable.aspx"&gt;previous entry&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Let’s first figure out what is a scenario when the RuntimeBinder should use the compile time type of the variable instead of it’s runtime type.&lt;/P&gt;
&lt;P&gt;Consider the code below:&lt;/P&gt;
&lt;DIV id=codeSnippetWrapper class=csharpcode-wrapper&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  1: &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; Base
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  2: { 
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  3:     &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; Foo(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; s) {}   
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  4: }   
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  5: &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; Derived : Base   
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  6: {   
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  7:     &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; Foo(&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; x) {}   
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  8: }  
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  9: 
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt; 10: Base obj = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; Derived();  
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt; 11: dynamic d = 4;  
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt; 12: 
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt; 13: obj.Foo(d);&lt;/PRE&gt;&lt;/PRE&gt;
&lt;DIV class=csharpcode&gt;At line 11 we instantiate a new Derived class and stick it into a variable of type Base. From the compiler’s perspective, this means when doing overload resolution it should only look at the methods defined by the Base. With that logic, the only method that the compiler should consider for overload resolution is Foo(string).&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;Because this method call is dispatched dynamically, the RuntimeBinder &lt;STRONG&gt;MUST&lt;/STRONG&gt; consider the same set of overloads. If the RuntimeBinder would have used the runtime-type for obj, the methods that would be considered when doing overload resolution would be Foo(string) and Foo(int) since the runtime-type of obj is Derived. &lt;/P&gt;
&lt;P&gt;This would lead to some very confusing cases when you are expecting one method to be called and at runtime a different one gets called.&lt;/P&gt;
&lt;P&gt;The same reasoning applies to passing arguments that are not typed dynamic to a method call that is dispatched dynamically.&lt;/P&gt;
&lt;P&gt;To summarize, there are two cases when the static type of a variable is passed to the RuntimeBinder:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;If the receiver of the call is not typed dynamic but at least one of its arguments is typed dynamic. 
&lt;UL&gt;
&lt;LI&gt;We need to have at least a dynamic argument in the call in order to have the call dispatch dynamically &lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;
&lt;LI&gt;If any of the arguments to a dynamically dispatched method call are not typed as dynamic. 
&lt;UL&gt;
&lt;LI&gt;An example might explain this better: &lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; x=4;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;dynamic&lt;/SPAN&gt; d = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; MyClass();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;d.Foo(x);&lt;/PRE&gt;&lt;/PRE&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;PRE&gt;&lt;FONT face="Trebuchet MS"&gt;Because x is not typed as dynamic, the compiler will pass the information about it’s static type to the RuntimeBinder&lt;/FONT&gt;&lt;/PRE&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;The compiler encodes the static type of a variable and passes that information to the RuntimeBinder by using the CallSite object &lt;A href="http://blogs.msdn.com/alexghi/archive/2009/05/19/how-to-dynamic-without-dynamic.aspx" mce_href="http://blogs.msdn.com/alexghi/archive/2009/05/19/how-to-dynamic-without-dynamic.aspx"&gt;we talked about here&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Feel free to post any questions you might have.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9948101" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/C_2300_+4-0/">C# 4.0</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/dynamic/">dynamic</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/code/">code</category></item><item><title>When does the compiler dispatch a call dynamically?</title><link>http://blogs.msdn.com/b/alexghi/archive/2010/01/04/when-does-the-compiler-dispatch-a-call-dynamically.aspx</link><pubDate>Mon, 04 Jan 2010 20:18:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9943557</guid><dc:creator>Alex Ghiondea - MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/alexghi/rsscomments.aspx?WeblogPostID=9943557</wfw:commentRss><comments>http://blogs.msdn.com/b/alexghi/archive/2010/01/04/when-does-the-compiler-dispatch-a-call-dynamically.aspx#comments</comments><description>&lt;P&gt;There are 2 cases when a call will be dispatched dynamically:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;When the receiver of the call is statically typed as dynamic 
&lt;DIV id=codeSnippetWrapper class=csharpcode-wrapper&gt;
&lt;DIV id=codeSnippet class=csharpcode&gt;&lt;PRE class=alt&gt;&lt;SPAN id=lnum1 class=lnum&gt;   1:&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;dynamic&lt;/SPAN&gt; d = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; &lt;FONT color=#008080&gt;MyClass&lt;/FONT&gt;();&lt;/PRE&gt;&lt;!--CRLF--&gt;&lt;PRE class=alteven&gt;&lt;SPAN id=lnum2 class=lnum&gt;   2:&lt;/SPAN&gt; d.Foo();&lt;/PRE&gt;&lt;!--CRLF--&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;When any of the arguments to the call are statically typed as dynamic 
&lt;DIV id=codeSnippetWrapper class=csharpcode-wrapper&gt;
&lt;DIV id=codeSnippet class=csharpcode&gt;&lt;PRE class=alt&gt;&lt;SPAN id=lnum1 class=lnum&gt;   1:&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;var&lt;/SPAN&gt; o = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; &lt;FONT color=#008080&gt;MyClass&lt;/FONT&gt;();&lt;/PRE&gt;&lt;!--CRLF--&gt;&lt;PRE class=alteven&gt;&lt;SPAN id=lnum2 class=lnum&gt;   2:&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;dynamic&lt;/SPAN&gt; d = 4;&lt;/PRE&gt;&lt;!--CRLF--&gt;&lt;PRE class=alt&gt;&lt;SPAN id=lnum3 class=lnum&gt;   3:&lt;/SPAN&gt; o.Bar(d);&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;By keeping the rules simple we hope to avoid cases where&amp;nbsp;there is confusion about whether or not a call is dispatched dynamically.&lt;/P&gt;
&lt;STYLE type=text/css&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;
&lt;/STYLE&gt;&lt;/STYLE&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9943557" width="1" height="1"&gt;</description></item><item><title>Compile time type vs runtime type of a variable</title><link>http://blogs.msdn.com/b/alexghi/archive/2009/05/20/compile-time-type-vs-runtime-type-of-a-variable.aspx</link><pubDate>Wed, 20 May 2009 02:51:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9630250</guid><dc:creator>Alex Ghiondea - MSFT</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/alexghi/rsscomments.aspx?WeblogPostID=9630250</wfw:commentRss><comments>http://blogs.msdn.com/b/alexghi/archive/2009/05/20/compile-time-type-vs-runtime-type-of-a-variable.aspx#comments</comments><description>&lt;P&gt;Let’s consider this code for a moment:&lt;/P&gt;
&lt;DIV class=csharpcode&gt;&lt;PRE class=alt&gt;&lt;SPAN class=lnum&gt;   1:  &lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;abstract&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;class&lt;/SPAN&gt; Animal { } &lt;/PRE&gt;&lt;PRE&gt;&lt;SPAN class=lnum&gt;   2:  &lt;/SPAN&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE class=alt&gt;&lt;SPAN class=lnum&gt;   3:  &lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;class&lt;/SPAN&gt; Tiger : Animal { } &lt;/PRE&gt;&lt;PRE&gt;&lt;SPAN class=lnum&gt;   4:  &lt;/SPAN&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE class=alt&gt;&lt;SPAN class=lnum&gt;   5:  &lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;class&lt;/SPAN&gt; Elephant : Animal { } &lt;/PRE&gt;&lt;PRE&gt;&lt;SPAN class=lnum&gt;   6:  &lt;/SPAN&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE class=alt&gt;&lt;SPAN class=lnum&gt;   7:  &lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;class&lt;/SPAN&gt; MyClass &lt;/PRE&gt;&lt;PRE&gt;&lt;SPAN class=lnum&gt;   8:  &lt;/SPAN&gt;{ &lt;/PRE&gt;&lt;PRE class=alt&gt;&lt;SPAN class=lnum&gt;   9:  &lt;/SPAN&gt;    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Main() &lt;/PRE&gt;&lt;PRE&gt;&lt;SPAN class=lnum&gt;  10:  &lt;/SPAN&gt;    { &lt;/PRE&gt;&lt;PRE class=alt&gt;&lt;SPAN class=lnum&gt;  11:  &lt;/SPAN&gt;        Animal a = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; Tiger();  &lt;/PRE&gt;&lt;PRE&gt;&lt;SPAN class=lnum&gt;  12:  &lt;/SPAN&gt;    } &lt;/PRE&gt;&lt;PRE class=alt&gt;&lt;SPAN class=lnum&gt;  13:  &lt;/SPAN&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;STYLE type=text/css&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;Now answer these two questions:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;What is the compile time type of the variable a? &lt;/LI&gt;
&lt;LI&gt;What is the runtime time of the variable a? &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;If your answer is: &lt;/P&gt;
&lt;P&gt;Compile time type is Animal and runtime type is Tiger then you are right on the money!&lt;/P&gt;
&lt;P&gt;Why is that?&lt;/P&gt;
&lt;P&gt;Because, for the compiler, the type of the variable is the type you used to define it. In this case, your variable is defined as type Animal, which means that the compile time type of it is Animal.&lt;/P&gt;
&lt;P&gt;However, you are assigning to that variable a new instance of type Tiger. This means that, when you are running the program, a memory location will be created to store a variable of type Tiger. Also, if you look at the definition for type Animal, you can see that the type is declared as Abstract and an abstract type cannot be instantiated.&lt;/P&gt;
&lt;P&gt;Why is this important? Well, in C# 4.0, the Runtime Binder that we introduced uses the runtime type of variables instead of the compile time type when doing overload resolution.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9630250" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/C_2300_+4-0/">C# 4.0</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/dynamic/">dynamic</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/code/">code</category></item><item><title>How-To: Dynamic without dynamic</title><link>http://blogs.msdn.com/b/alexghi/archive/2009/05/19/how-to-dynamic-without-dynamic.aspx</link><pubDate>Tue, 19 May 2009 02:53:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9626445</guid><dc:creator>Alex Ghiondea - MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/alexghi/rsscomments.aspx?WeblogPostID=9626445</wfw:commentRss><comments>http://blogs.msdn.com/b/alexghi/archive/2009/05/19/how-to-dynamic-without-dynamic.aspx#comments</comments><description>&lt;P&gt;I am sure that by now you all heard about the new C# 4.0 feature called dynamic. In short, dynamic is the new feature in C# 4.0 that allows you to defer member binding until runtime.&lt;/P&gt;
&lt;P&gt;The way you use this feature is very straight forward. If you have a class C with a method M and you want to call the method M but defer the binding until runtime, you would write this:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;DIV class=csharpcode&gt;
&lt;DIV class=csharpcode&gt;&lt;PRE class=alt&gt;&lt;SPAN class=lnum&gt;   1:  &lt;/SPAN&gt;&lt;FONT color=#0000ff&gt;dynamic&lt;/FONT&gt; d = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; C();&lt;/PRE&gt;&lt;PRE&gt;&lt;SPAN class=lnum&gt;   2:  &lt;/SPAN&gt;d.M();&lt;/PRE&gt;&lt;/DIV&gt;
&lt;STYLE type=text/css&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;
&lt;/DIV&gt;
&lt;STYLE type=text/css&gt;








.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P mce_keep="true"&gt;What happens is that the compiler will notice that you are trying to do a late-bound call and will generate some code behind the scenes that will capture all the necessary information to allow the late binding to succeed.&lt;/P&gt;
&lt;P&gt;The information is captured inside a CallSite object, and this is what such an object contains:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The type of the operation you want to bind (method call, property set, etc) &lt;/LI&gt;
&lt;LI&gt;Is the method you are calling static &lt;/LI&gt;
&lt;LI&gt;The name of the method you want to call &lt;/LI&gt;
&lt;LI&gt;The context where the call was made (the class that contains the call). This is used to determine if a call to a private method should be allowed &lt;/LI&gt;
&lt;LI&gt;The type arguments of the method (if any) &lt;/LI&gt;
&lt;LI&gt;The type of all the arguments for the method call &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;There is nothing stopping you from creating such a structure by yourself in your code. You would have to write something&amp;nbsp;like this:&lt;/P&gt;
&lt;DIV class=csharpcode&gt;&lt;PRE class=alt&gt;&lt;SPAN class=lnum&gt;   1:  &lt;/SPAN&gt;&lt;FONT color=#0000ff&gt;var&lt;/FONT&gt; callM = &lt;FONT color=#408080&gt;CallSite&lt;/FONT&gt;&amp;lt;&lt;FONT color=#008080&gt;Action&lt;/FONT&gt;&amp;lt;&lt;FONT color=#008080&gt;CallSite&lt;/FONT&gt;, &lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt;&amp;gt;&amp;gt;.Create(&lt;/PRE&gt;&lt;PRE&gt;&lt;SPAN class=lnum&gt;   2:  &lt;/SPAN&gt;    &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; &lt;FONT color=#408080&gt;CSharpInvokeMemberBinder&lt;/FONT&gt;(&lt;/PRE&gt;&lt;PRE class=alt&gt;&lt;SPAN class=lnum&gt;   3:  &lt;/SPAN&gt;        &lt;FONT color=#408080&gt;CSharpCallFlags&lt;/FONT&gt;.None, &lt;/PRE&gt;&lt;PRE&gt;&lt;SPAN class=lnum&gt;   4:  &lt;/SPAN&gt;        &lt;SPAN class=str&gt;&lt;FONT color=#800000&gt;"M"&lt;/FONT&gt;&lt;/SPAN&gt;, &lt;/PRE&gt;&lt;PRE class=alt&gt;&lt;SPAN class=lnum&gt;   5:  &lt;/SPAN&gt;        &lt;SPAN class=kwrd&gt;typeof&lt;/SPAN&gt;(&lt;FONT color=#008080&gt;Program&lt;/FONT&gt;), &lt;/PRE&gt;&lt;PRE&gt;&lt;SPAN class=lnum&gt;   6:  &lt;/SPAN&gt;        &lt;SPAN class=kwrd&gt;null&lt;/SPAN&gt;, &lt;/PRE&gt;&lt;PRE class=alt&gt;&lt;SPAN class=lnum&gt;   7:  &lt;/SPAN&gt;        &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; &lt;FONT color=#008080&gt;CSharpArgumentInfo&lt;/FONT&gt;[] { &lt;/PRE&gt;&lt;PRE&gt;&lt;SPAN class=lnum&gt;   8:  &lt;/SPAN&gt;                &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; &lt;FONT color=#008080&gt;CSharpArgumentInfo&lt;/FONT&gt;(&lt;FONT color=#008080&gt;CSharpArgumentInfoFlags&lt;/FONT&gt;.None, &lt;SPAN class=kwrd&gt;null&lt;/SPAN&gt;) &lt;/PRE&gt;&lt;PRE class=alt&gt;&lt;SPAN class=lnum&gt;   9:  &lt;/SPAN&gt;        }&lt;/PRE&gt;&lt;PRE&gt;&lt;SPAN class=lnum&gt;  10:  &lt;/SPAN&gt;    )&lt;/PRE&gt;&lt;PRE class=alt&gt;&lt;SPAN class=lnum&gt;  11:  &lt;/SPAN&gt;);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;STYLE type=text/css&gt;





.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;STYLE type=text/css&gt;







.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;Besides all the information that is captured already, one interesting bit of information is&amp;nbsp;decoded from using the Action delegate. By using Action we are specifying that we don’t care about the result of the operation (or that the return type of the method is void). If we did want to get back a result from the operation, we would have had to use Func instead of Action.&lt;/P&gt;
&lt;P&gt;Now that all the necessary information is available inside the callM object we can execute the method using late binding. To do that, we simply do this:&lt;/P&gt;
&lt;DIV class=csharpcode&gt;&lt;PRE class=alt&gt;&lt;SPAN class=lnum&gt;   1:  &lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; obj = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; C();&lt;/PRE&gt;&lt;PRE&gt;&lt;SPAN class=lnum&gt;   2:  &lt;/SPAN&gt;callM.Target(callM, obj);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;STYLE type=text/css&gt;



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;STYLE type=text/css&gt;



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;f you run this code you will be calling the method M using late binding. And that’s how&amp;nbsp;you do dynamic without dynamic&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9626445" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/C_2300_+4-0/">C# 4.0</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/dynamic/">dynamic</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/code/">code</category></item><item><title>C# 4.0 - Dynamic internals talk</title><link>http://blogs.msdn.com/b/alexghi/archive/2009/04/07/c-4-0-dynamic-internals-talk.aspx</link><pubDate>Tue, 07 Apr 2009 21:19:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9536733</guid><dc:creator>Alex Ghiondea - MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/alexghi/rsscomments.aspx?WeblogPostID=9536733</wfw:commentRss><comments>http://blogs.msdn.com/b/alexghi/archive/2009/04/07/c-4-0-dynamic-internals-talk.aspx#comments</comments><description>&lt;P&gt;Yesterday I gave a talk to the &lt;A href="http://dotnetda.com/" mce_href="http://dotnetda.com/"&gt;Redmond .NET user group&lt;/A&gt; where I talked about the C# 4.0 dynamic feature. The talk focused on how the feature is implemented and how all the pieces go together.&lt;/P&gt;
&lt;P&gt;About 30 people showed up and stayed for the 1 hour and 40 minutes of dynamic talk.&lt;/P&gt;
&lt;P&gt;According to the feedback I got, people enjoyed the technical talk, and that made me feel good [:d].&lt;/P&gt;
&lt;P&gt;I have attached the slide deck if you are interested.&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9536733" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-09-53-67-33/C_2300_-4.0-_2D00_-Dynamic-internals.ppsx" length="149039" type="application/vnd.openxmlformats-officedocument.pres" /><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/C_2300_+4-0/">C# 4.0</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/dynamic/">dynamic</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/presentation/">presentation</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/code/">code</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/Redmond/">Redmond</category></item><item><title>Using Anonymous types to deserialize JSON data</title><link>http://blogs.msdn.com/b/alexghi/archive/2008/12/22/using-anonymous-types-to-deserialize-json-data.aspx</link><pubDate>Tue, 23 Dec 2008 00:58:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9248449</guid><dc:creator>Alex Ghiondea - MSFT</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/alexghi/rsscomments.aspx?WeblogPostID=9248449</wfw:commentRss><comments>http://blogs.msdn.com/b/alexghi/archive/2008/12/22/using-anonymous-types-to-deserialize-json-data.aspx#comments</comments><description>&lt;P&gt;While I was playing around with deserializing objects written in JSON format, I noticed that I had to write a lot of classes that were used just to deserialize the JSON response. I noticed that I did not necessarily needed those classes, and that I often would just transfer data from those classes back into my own objects. Basically, I ended up with a lot of classes which were used just to get the JSON response into objects.&lt;/P&gt;
&lt;P&gt;I started thinking about how I could leverage C# language features to accomplish the same goals. I this article I am going to show you how you can use the C# 3.0 anonymous types feature to deserialize JSON data back into objects. For parsing JSON and assigning values to my object I will use this &lt;A href="http://www.codeplex.com/Json/Release/ProjectReleases.aspx?ReleaseId=18825" mce_href="http://www.codeplex.com/Json/Release/ProjectReleases.aspx?ReleaseId=18825"&gt;JSON implementation&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;C# 2.0&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The JSON text that I am trying to parse is this: &lt;/P&gt;
&lt;P&gt;&lt;FONT color=#0000ff&gt;string&lt;/FONT&gt; JSONText =&lt;FONT color=#400000&gt; &lt;/FONT&gt;&lt;FONT color=#800000&gt;"{Text:\"Hello World!!\", Status:\"Ok\"}"&lt;/FONT&gt;;&lt;/P&gt;
&lt;P&gt;In order to do this, I would have to create a class (with any name) that will have the two properties Text and Status:&lt;/P&gt;&lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;FONT color=#408080&gt;HelloWorldResponse&lt;/FONT&gt; &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;public string&lt;/FONT&gt; Text { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; } &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;public string&lt;/FONT&gt; Status { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; } &lt;BR&gt;} 
&lt;P&gt;And now, in order to deserialize the data, I use the JSON serializer like so:&lt;/P&gt;&lt;FONT color=#0000ff&gt;var&lt;/FONT&gt; x = js.Deserialize(&lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; &lt;FONT color=#008080&gt;StringReader&lt;/FONT&gt;(JSONText), &lt;FONT color=#0000ff&gt;typeof&lt;/FONT&gt;(&lt;FONT color=#008080&gt;HelloWorldResponse&lt;/FONT&gt;)) &lt;FONT color=#0000ff&gt;as&lt;/FONT&gt; &lt;FONT color=#008080&gt;HelloWorldResponse&lt;/FONT&gt;; 
&lt;P&gt;&lt;STRONG&gt;C# 3.0&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;In C# 3.0 we introduced the notion of anonymous types - types that have no name that you can speak - that can have any shape you want. The main reason why they were introduced is to support LINQ, but they can be used in other way as well.&lt;/P&gt;
&lt;P&gt;So, in C# 3.0 instead of writing my own classes, I decided to let the compiler do the work for me. What I will do is to create the anonymous types in the function that need that response. I will pass the anonymous type to the JSON deserializer and get back an object that contains an anonymous type populated with the data from the JSON string. To get from object to the strongly typed anonymous type, I will use a generic method that casts the object to the type of the anonymous type .&lt;/P&gt;
&lt;P&gt;Here is the method that converts object to the anonymous type:&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#0000ff&gt;public static class&lt;/FONT&gt; &lt;FONT color=#008080&gt;TypeExtension&lt;/FONT&gt; &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;public static&lt;/FONT&gt; T ToType&amp;lt;T&amp;gt;(&lt;FONT color=#0000ff&gt;this object&lt;/FONT&gt; o, T typeToCastTo) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;return&lt;/FONT&gt; (T)o; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;}&lt;/P&gt;
&lt;P&gt;This is a generic extension method that takes in two arguments: the object I am trying to convert and the type I am converting to. The compiler will do type inference on the argument going in, so if I pass in a variable of an anonymous type, it will figure out the unspeakable name for the anonymous type. Then, it just returns the result of the converson from the object to that type.&lt;/P&gt;
&lt;P&gt;Then, I can just write the code like this:&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#0000ff&gt;var&lt;/FONT&gt; helloResponse = &lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; { Text = string.Empty, Status = string.Empty }; &lt;BR&gt;&lt;FONT color=#0000ff&gt;var&lt;/FONT&gt; y = js.Deserialize(&lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; &lt;FONT color=#008080&gt;StringReader&lt;/FONT&gt;(JSONText), helloResponse.GetType()).ToType(helloResponse);&lt;/P&gt;
&lt;P mce_keep="true"&gt;And then, y will hold a reference to an anonymous type and I can use it to read the properties from the type.&lt;/P&gt;
&lt;P mce_keep="true"&gt;The advantage that I am getting is not in the number of classes that I have to write - the compiler will generate the same number of classes* as I would have.&amp;nbsp; The disadvantage to this solution is the fact that now I have type definitions inside methods, which can make it harder to debug/change. Also, if your internal classes map directly to the response you get as JSON, then you are probably better off using "real classes".&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;STRONG&gt;C# 4.0&lt;/STRONG&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;In C# 4.0, with the introduction of dynamic, we can further simplify this code. In C# 4.0, I don't need the method that will convert from object to the anonymous type. I can just stick the object returned from the JSON deserializer into a dynamic and use dynamic dispatch to get to the members that I care about:&lt;/P&gt;&lt;FONT color=#0000ff&gt;var&lt;/FONT&gt; helloResponse = &lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; { Text = string.Empty, Status = string.Empty };&amp;nbsp; &lt;BR&gt;&lt;FONT color=#0000ff&gt;&lt;STRONG&gt;dynamic&lt;/STRONG&gt;&lt;/FONT&gt; y = js.Deserialize(&lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; &lt;FONT color=#008080&gt;StringReader&lt;/FONT&gt;(JSONText), helloResponse.GetType()); 
&lt;P mce_keep="true"&gt;The disadvantage of this solution is that you will not have IntelliSense one you go dynamic, so any spelling mistake will not be caught by the compiler.&lt;/P&gt;
&lt;P mce_keep="true"&gt;So here you go - 3 ways of doing the same thing, each with their pros and cons. I really like to C# 3.0/ C# 4.0 versions where you create the type "on the fly" and then you go ahead and use result. I still don't like the fact that I need to explicitly state what are the members on the type. I would just like to get "a" type back and then magically get just the members I care about. Well - we'll see :).&lt;/P&gt;
&lt;P mce_keep="true"&gt;*the compiler will try to reuse similar anonymous types, so you might actually get fewer types&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9248449" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/C_2300_+4-0/">C# 4.0</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/dynamic/">dynamic</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/json/">json</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/C_2300_+3-0/">C# 3.0</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/code/">code</category></item><item><title>C# 4.0 language features at Seattle Code Camp</title><link>http://blogs.msdn.com/b/alexghi/archive/2008/11/15/c-4-0-presentation-at-seattle-code-camp.aspx</link><pubDate>Sat, 15 Nov 2008 23:08:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9102079</guid><dc:creator>Alex Ghiondea - MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/alexghi/rsscomments.aspx?WeblogPostID=9102079</wfw:commentRss><comments>http://blogs.msdn.com/b/alexghi/archive/2008/11/15/c-4-0-presentation-at-seattle-code-camp.aspx#comments</comments><description>&lt;P&gt;I just came back from Seattle Code Camp where I gave a talk on C# 4.0.&amp;nbsp;&lt;A class="" href="https://seattle.codecamp.us/default.aspx" mce_href="https://seattle.codecamp.us/default.aspx"&gt;Seattle Code Camp&lt;/A&gt; is a community organized event where people meet and talk about code. &lt;/P&gt;
&lt;P&gt;My&amp;nbsp;presentation&amp;nbsp;was about the new language features&amp;nbsp;we are introducing in&amp;nbsp;C#&amp;nbsp;4.0. I talked about C# dynamic, named and optional parameters and COM interop. People were very interested about what you can do with the dynamic feature (i.e. call into Pyhon)&amp;nbsp;and very&amp;nbsp;happy about the COM interop support we are adding (i.e. named and optional). I really enjoeyd talking to fellow coders, eating pizza&amp;nbsp;and&amp;nbsp;just beeing there. I will most likely come back next year!&lt;/P&gt;
&lt;P&gt;But wait! - The camp is not over yet!!&lt;/P&gt;
&lt;P&gt;If my talk made you think about dynamic languages, Sunday is a great time to learn more. There are some interesting talks about dynamic languages in the Van Gogh room the entire day.&lt;/P&gt;
&lt;P&gt;And, if you like LINQ and want to learn more about it you should&amp;nbsp;go see the &lt;A class="" href="https://seattle.codecamp.us/Sessions.aspx#Creating+LINQ+providers" mce_href="https://seattle.codecamp.us/Sessions.aspx#Creating+LINQ+providers"&gt;talk&lt;/A&gt; that my colleague Marcelo is giving from 1:15 in the Al-Khwarismi&amp;nbsp;room.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;[Edit]&lt;/STRONG&gt; Added the slides for the presentation to this post&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9102079" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-09-10-20-79/C_2300_-4.0-language-feature_7F00_s.pptx" length="175446" type="application/vnd.openxmlformats-officedocument.pres" /><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/Code+Camp/">Code Camp</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/C_2300_+4-0/">C# 4.0</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/dynamic/">dynamic</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/Seattle/">Seattle</category><category domain="http://blogs.msdn.com/b/alexghi/archive/tags/presentation/">presentation</category></item></channel></rss>