Is it possible to convert a lambda expression of some return type to its equivalent expression of a more specific type?

青春壹個敷衍的年華 提交于 2019-12-11 08:22:04

问题


public class Entity
{
    public int A { get; set; }
    public string B { get; set; }
}

Expression<Func<Entity,object>> expr = x => x.A;
Expression<Func<Entity,int>> exprAtRuntime = x => x.A;

Expression<Func<Entity,object>> expr2 = x => x.B;
Expression<Func<Entity,string>> expr2AtRuntime = x => x.B;

How can I take expr and convert it to the type of expr2 at runtime?

Likewise, I need to do the same for property B. I need to accept a params array of type Expression<Func<Entity,object>> which represents accessing different properties of an object, so they all need to have the Entity type and the general return type of object (as answered in this precursor question).

But from that expression, I need to construct it's more strongly-typed version for each element of the params array (i.e. convert Expression<Func<TEntity,object>> to a type specific to the underlying property being accessed Expression<Func<TEntity,TProperty>>, using information about the type of the property being accessed on the entity type. How can this be done?


回答1:


To do this, you can use an ExpressionVisitor that removes the Convert (if it exists) and then fixes up the delegate type:

class RemoveConvertVisitor : ExpressionVisitor
{
    protected override Expression VisitUnary(UnaryExpression node) =>
        node.NodeType == ExpressionType.Convert ? node.Operand : base.VisitUnary(node);

    protected override Expression VisitLambda<T>(Expression<T> node) =>
        Expression.Lambda(Visit(node.Body), node.Parameters);
}


来源:https://stackoverflow.com/questions/43815289/is-it-possible-to-convert-a-lambda-expression-of-some-return-type-to-its-equival

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!