Niels Harremoes
Niels Harremoes

Reputation: 390

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

Upvotes: 1

Views: 651

Answers (2)

Davide Icardi
Davide Icardi

Reputation: 12209

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.

Upvotes: 1

EnderWiggin
EnderWiggin

Reputation: 515

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.

Upvotes: 0

Related Questions