title: How can I use dynamic variables with anonymous types?
body: <p>I am trying to use dynamic variables containing anonymous types. For example:</p>
  
  <pre><code>var hello = new { french = "bonjour" };
  // alternative, dynamic hello
  // var hello = new ExpandoObject();
  // hello.french = hello
  
  dynamic x = new ExpandoObject();
  x.a = hello;
  
  // alternative, anonymous x
  // var x = new { a = hello };
  
  Assert.AreEqual("bonjour", x.a.french); // Sanity check - never fails
  
  var interpreter = new Interpreter();
  interpreter.SetVariable("x", x);
  
  var result = interpreter.Eval&lt;string&gt;("x.a.french"); // fails
  Assert.AreEqual("bonjour", result);
  </code></pre>
  
  <p>However, I get the error</p>
  
  <blockquote>
    <p>Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does
    not contain a definition for 'french'</p>
  </blockquote>
  
  <p>So apparently, x.a does not have the correct type?
  If I introduce a named class, Translate, and change the first line to </p>
  
  <pre><code>var hello = new Translate {french = "bonjour"};
  </code></pre>
  
  <p>it works as expected. It also works if I replace make both x and hello anonymous by replacing lines 2 and 3 with</p>
  
  <pre><code>var x = new { a = hello };
  </code></pre>
  
  <p>or if I make both x and hello dynamic.</p>
  
  <p>If I make x anonymous and hello dynamic, it also fails:</p>
  
  <pre><code>    dynamic hello = new ExpandoObject();
      hello.french = "bonjour";
      var x = new { a = hello} ;
  </code></pre>
  
  <p>Here, I get the error</p>
  
  <blockquote>
    <p>DynamicExpresso.Exceptions.ParseException: No property or field
    'french' exists in type 'Object'</p>
  </blockquote>
  
  <p>Is there any way to make my first code sample work? In real life, I am trying something somewhat more complex.</p>
  
  <p>I also have problems trying to access arrays:</p>
  
  <pre><code>dynamic dyn = new ExpandoObject();
  
  dyn.Foo = new int[] {42};
  var interpreter = new Interpreter()
              .SetVariable("dyn", (object)dyn);
  
  Assert.AreEqual(dyn.Foo[0], interpreter.Eval("dyn.Foo[0]"));
  </code></pre>