1

I am trying to use dynamic variables containing anonymous types. For example:

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<string>("x.a.french"); // fails
Assert.AreEqual("bonjour", result);

However, I get the error

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'french'

So apparently, x.a does not have the correct type? If I introduce a named class, Translate, and change the first line to

var hello = new Translate {french = "bonjour"};

it works as expected. It also works if I replace make both x and hello anonymous by replacing lines 2 and 3 with

var x = new { a = hello };

or if I make both x and hello dynamic.

If I make x anonymous and hello dynamic, it also fails:

    dynamic hello = new ExpandoObject();
    hello.french = "bonjour";
    var x = new { a = hello} ;

Here, I get the error

DynamicExpresso.Exceptions.ParseException: No property or field 'french' exists in type 'Object'

Is there any way to make my first code sample work? In real life, I am trying something somewhat more complex.

I also have problems trying to access arrays:

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]"));
Niels Harremoes
  • 320
  • 1
  • 18
  • Try to look here: https://github.com/davideicardi/DynamicExpresso/blob/master/test/DynamicExpresso.UnitTest/DynamicTest.cs Consider that this works only on .NET 4.7 and with version 1.3 or later – Davide Icardi Oct 29 '18 at 16:04
  • Thanks - 4.7 is fine. I've taken a look, but I still cannot make it work. For example, adding this test fails (sorry about the missing formatting) [Test] public void Get_Property_of_a_nested_Anonymous() { dynamic dyn = new ExpandoObject(); dyn.Sub = new { Foo = "bar"}; var interpreter = new Interpreter() .SetVariable("dyn", dyn); Assert.AreEqual(dyn.Sub.Foo, interpreter.Eval("dyn.Sub.Foo")); } – Niels Harremoes Oct 29 '18 at 18:18
  • I have tracked it down to Parser.ParseDynamicProperty - apparently it chooses the "wrong" binder? This is probably due to the two-pass approach - first we parse and type-analyse the code, then we run it. During parsing we decide that the type of dyn.Sub is object and generate a binder based on that? – Niels Harremoes Oct 29 '18 at 18:54
  • 1
    Uhm yes, probably this is not supported. Can i ask you to open an issue with your unit test and the current SO link? Thanks – Davide Icardi Oct 29 '18 at 22:04
  • I have created two issues - one for the anonymous class issue, one for indexers. Do you have any tips for workarounds? I wouldn't mind writing my own DynamicObject-derived class, but I would really like to support expressions like myObject.dynamicName.someProperty[1] – Niels Harremoes Oct 30 '18 at 08:47

0 Answers0