Using dynamic types with expresso

644 Views Asked by At

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

2

There are 2 best solutions below

1
On BEST ANSWER

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.

0
On

Expression Evaluator supports dynamics (ExpandoObject). It supports method calls, property and index accessors, get and set. If you do encounter an error with dynamics please let me know as dynamics is relatively newly supported.