开发者

Assign a value from an expression mapping

开发者 https://www.devze.com 2023-02-07 11:32 出处:网络
I map properties using Expression<Func<TEntity, object>>. I\'ve just needed to fetch property values from objects until know. 开发者_运维技巧How can I assign values using that expression?

I map properties using Expression<Func<TEntity, object>>.

I've just needed to fetch property values from objects until know. 开发者_运维技巧How can I assign values using that expression?


try this, assuming 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;

then just use SetValue

0

精彩评论

暂无评论...
验证码 换一张
取 消