C# Eval() support

Adi Barda picture Adi Barda · Jun 21, 2009 · Viewed 64.4k times · Source

we need to evaluate a value in an object in run time while we have a textual statement of the exact member path for example: myobject.firstMember.secondMember[3].text
we thought of parsing this textual statement using regex and then evaluate the text value by using reflection but before we do that i wonder if C# support some kind of eval ability? so we won't have to do the parsing ourself. How do microsoft do this in their immediate window or watch windows?

thanks you very much,

Adi Barda

Answer

Alex Yakunin picture Alex Yakunin · Jun 21, 2009

Probably the easiest way is to use DataBinder.Eval from System.Web.UI:

var foo = new Foo() { Bar = new Bar() { Value = "Value" } };
var value = DataBinder.Eval(foo, "Bar.Value");