title: Possible bug in C# dynamic?
body: <p>I have encountered issues with using dynamic variables in C#. This arose while coding NancyFx routing modules, but I have boiled down the issue to the example below. While I received a different exception in the original code, the example code still throws an exception that I believe is erroneous. Does anyone see what is going on here, or have others encountered similar problems?</p>
  
  <p>Note that the following posts may be related:
  <a href="https://stackoverflow.com/questions/22672775/net-c-framework-bug">StackOverflowException when accessing member of generic type via dynamic: .NET/C# framework bug?</a>
  <a href="https://stackoverflow.com/questions/2431227/system-dynamic-bug">System.Dynamic bug?</a></p>
  
  <p>The code:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          var dictionary = new Dictionary&lt;string, object&gt;();
          dictionary.Add("number", 12);
          var result = MethodUsesExplicitDeclaration(dictionary);
          var result2 = MethodUsesImplicitDeclaration(dictionary);
      }
  
      static dynamic MethodUsesExplicitDeclaration(dynamic reallyDictionary)
      {
          // this works, ostensibly because the local variable is explicitly declared
          IDictionary&lt;string, object&gt; dictionary = CastDictionary(reallyDictionary);
          return dictionary.Get&lt;int&gt;("number");
      }
  
      static dynamic MethodUsesImplicitDeclaration(dynamic reallyDictionary)
      {
          // this throws an exception, and the only difference is 
          // that the variable declaration is implicit
          var dictionary = CastDictionary(reallyDictionary);
          return dictionary.Get&lt;int&gt;("number");
      }
  
      static IDictionary&lt;string, object&gt; CastDictionary(dynamic arg)
      {
          return arg as IDictionary&lt;string, object&gt;;
      }
  }
  
  static class Extensions
  {
      public static T Get&lt;T&gt;(this IDictionary&lt;string, object&gt; dictionary, string key)
      {
          var value = dictionary[key];
          if (value is T)
              return (T)value;
          throw new InvalidOperationException();
      }
  }
  </code></pre>
  
  <p>The exception: <code>Microsoft.CSharp.RuntimeBinder.RuntimeBinderException was unhandled
    HResult=-2146233088
    Message='System.Collections.Generic.Dictionary&lt;string,object&gt;' does not contain a definition for 'Get'
    Source=Anonymously Hosted DynamicMethods Assembly
    StackTrace:
         at CallSite.Target(Closure , CallSite , Object , String )
         at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
         at DynamicBug.Program.MethodUsesImplicitDeclaration(Object reallyDictionary) in c:\TFS\UnreleasedCode\POC\DynamicBug\DynamicBug\Program.cs:line 28
         at DynamicBug.Program.Main(String[] args) in c:\TFS\UnreleasedCode\POC\DynamicBug\DynamicBug\Program.cs:line 16
    InnerException:
  </code></p>