Reputation: 27037
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?
Upvotes: 1
Views: 238
Reputation: 4307
Examples:
ExpressionEvaluator.GetValue("Test", "Length.ToString().Length");
ExpressionEvaluator.GetValue(null, "DateTime.Now")
Upvotes: 0
Reputation: 1237
You can't do it either with reflection or with dynamic keyword. What you can do is:
Upvotes: 2