【发布时间】:2012-03-20 19:57:16
【问题描述】:
在 C# 中,假设我有以下对象:
var myObject = new
{
Val = new[]
{
new { ArrVal = "three1"},
new { ArrVal = "three2"}
}
};
如何针对对象动态评估以下表达式? (表达式是一个字符串)
"Val[1].ArrVal"
在这种情况下,我希望表达式返回“three2”。我想写一个带有以下签名的方法:
object GetValue(string expression, object objectToExtractValueFrom)
我可以通过反射来做到这一点,或者以某种方式使用动态关键字吗?
【问题讨论】:
标签: .net reflection dynamic