从表达式映射中分配值

时间:2011-01-28 13:03:48

标签: c# .net-4.0 expression-trees

我使用Expression<Func<TEntity, object>>映射属性。

我只需要从对象中获取属性值,直到知道为止。如何使用该表达式分配值?

1 个答案:

答案 0 :(得分:1)

尝试此操作,假设为expression = Expression<Func<TEntity, object>>

MemberExpression memberExpression;

switch (expression.Body.NodeType)
{
  case ExpressionType.Convert:
    memberExpression = ((UnaryExpression) expression.Body).Operand as MemberExpression;
    break;
  case ExpressionType.MemberAccess:
    memberExpression = expression.Body as MemberExpression;
    break;
}

var propInfo = memberExpression.Member as PropertyInfo;

然后只使用SetValue