In C#, imagine I have the following object:
var myObject = new
{
Val = new[]
{
new { ArrVal = "three1"},
new { ArrVal = "three2"}
}
};
How would I dynamically evaluate the following expression against the object? (the expression is a string)
"Val[1].ArrVal"
In this case I would expect the expression to return “three2”. I’d like to write a method with the following signature:
object GetValue(string expression, object objectToExtractValueFrom)
Can I do this with reflection, or somehow with the dynamic keyword?
You can’t do it either with reflection or with dynamic keyword. What you can do is:
will be compiled and executed against your parameter(s)