Search code examples
dynamic-expresso

Using dynamic types with expresso


I would like to use a dynamic value as a parameter. E.g.

dynamic dyn = new ExpandoObject();
dyn.Foo = "bar";
var bar = new Interpreter().Eval("d.Foo", new Parameter("d", dyn));
Assert.AreEqual("bar", bar.ToString());

But I get an error saying "No property or field 'Foo' exists in type 'ExpandoObject'" ?

Is this supposed to be possible?

Regards, Niels


Solution

  • Unfortunately for now dynamics (ExpandoObject) are not supported. I will consider this feature for the next release.

    A possible workaround is to use anonymous objects:

    dynamic dyn = new ExpandoObject();
    dyn.Foo = "bar";
    
    var bar = new Interpreter().Eval("d.Foo", new Parameter("d", new { Foo = dyn.Foo }));
    

    Consider that in this case the property is evaluated when you create the parameter.

    You can also convert a dynamic into an anonymous type (see Cast ExpandoObject to anonymous type) but the result is not very different.

    Disclaimer: I'm the creator of Dynamic Expresso library.