简体   繁体   English

如何在C#中合并(或动态构建和组合)lambda表达式?

[英]How do I merge (or dynamically build and combine) lambda expressions in C#?

I'm trying to dynamically combin lambda expressions. 我正在尝试动态组合lambda表达式。 The code below will explain what I want. 下面的代码将解释我想要什么。 This is NOT a case of combining a=>b and b=>c to a=>c. 这不是将a => b和b => c组合为a => c的情况。 Instead, I want to prevent code duplication by reusing conversion-expressions: 相反,我想通过重用conversion-expressions来防止代码重复:

class User // convert from...
{
  public string FirstName {get;set;}
  public string LastName {get;set;}
}

class Person // convert to...
{
  public string Name
}

public class UserTransaction
{
  User FromUser {get;set;}
  User ToUser {get;set;}
  decimal Amount {get;set;}
}

public class PersonTransaction
{
  Person FromPerson {get;set;}
  Person ToPerson {get;set;}
  bool IsPositive;
}

Expression<Func<User, Person>> ToPerson = u => new Person {Name = u.FirstName + " " + u.LastName};
Expression<Func<UserTransaction, PersonTransaction>> PersonTransaction = ut => new PersonTransaction {
  FromPerson = FromUser.Compile()(ut.FromUser), // Actually, I do not want to compile
  ToPerson = ToUser.Compile()(ut.FromUser), // (or double code)
  IsPositive = ut.Amount > 0
}

In the example above, I already have an expression to convert a user to a person. 在上面的示例中,我已经有了一个将用户转换为人的表达式。 I do not want to duplicate this code or compile it. 我不想重复此代码或对其进行编译。 I've tried using stripping out the "compile"-calls by manually editing the expression tree. 我尝试通过手动编辑表达式树来剥离“编译”调用。 I did not succeed. 我没有成功。 Has anybody tried something similar and succeed? 有人尝试过类似的方法并且成功了吗?

You can do some voodoo with ExpressionVisitor to rewrite your existing code to be fully inline; 您可以使用ExpressionVisitor进行一些巫术改写以完全内联; this: 这个:

  • detects the invoke 检测调用
  • locates the Compile 定位编译
  • resolves the originating lambda 解析原始lambda
  • swaps in all the parameter values directly 直接交换所有参数值
  • rebuilds the expression tree accordingly 相应地重建表达式树

Have fun! 玩得开心!

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;

public class User // convert from...
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Person // convert to...
{
    public string Name { get; set; }
}

public class UserTransaction
{
    public User FromUser { get; set; }
    public User ToUser { get; set; }
    public decimal Amount { get; set; }
}

public class PersonTransaction
{
    public Person FromPerson { get; set; }
    public Person ToPerson { get; set; }
    public bool IsPositive { get; set; }
}

static class Program
{

    static void Main()
    {
        Expression<Func<User, Person>> ToPerson = u => new Person { Name = u.FirstName + " " + u.LastName };
        Expression<Func<UserTransaction, PersonTransaction>> PersonTransaction = ut => new PersonTransaction
        {
            FromPerson = ToPerson.Compile()(ut.FromUser), // Actually, I do not want to compile
            ToPerson = ToPerson.Compile()(ut.ToUser), // (or double code)
            IsPositive = ut.Amount > 0
        };
        var visitor = new RemoveCompilationsExpressionVisitor();


        var inlined = (Expression<Func<UserTransaction, PersonTransaction>>)visitor.Visit(PersonTransaction);
    }

    class ParameterSwapExpressionVisitor :ExpressionVisitor
    {
        private readonly Dictionary<ParameterExpression, Expression> swaps;

        public ParameterSwapExpressionVisitor(Dictionary<ParameterExpression, Expression> swaps)
        {
            this.swaps = swaps;
        }
        protected override Expression VisitParameter(ParameterExpression node)
        {
            Expression result;
            return swaps.TryGetValue(node, out result) ? result : base.VisitParameter(node);
        }
    }
    class RemoveCompilationsExpressionVisitor : ExpressionVisitor
    {
        protected override Expression VisitInvocation(InvocationExpression node)
        {
            var lambda = TryGetInnerLambda(node.Expression);
            if(lambda != null)
            {
                // this would be a partial solution, but we want to go further!
                // return Expression.Invoke(lambda, node.Arguments);

                var swaps = new Dictionary<ParameterExpression, Expression>();
                for(int i = 0; i < lambda.Parameters.Count; i++)
                {
                    swaps.Add(lambda.Parameters[i], node.Arguments[i]);
                }
                var visitor = new ParameterSwapExpressionVisitor(swaps);
                return visitor.Visit(lambda.Body);
            }
            return base.VisitInvocation(node);
        }

        LambdaExpression TryGetInnerLambda(Expression node)
        {
            try
            {
                if(node.NodeType == ExpressionType.Call)
                {
                    var mce = (MethodCallExpression)node;
                    var method = mce.Method;
                    if (method.Name == "Compile" && method.DeclaringType.IsGenericType && method.DeclaringType.GetGenericTypeDefinition()
                        == typeof(Expression<>))
                    {
                        object target;
                        if (TryGetLiteral(mce.Object, out target))
                        {
                            return (LambdaExpression)target;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                /* best effort only */
                Debug.WriteLine(ex);
            }
            return null;
        }

        static bool TryGetLiteral(Expression node, out object value)
        {
            value = null;
            if (node == null) return false;
            switch(node.NodeType)
            {
                case ExpressionType.Constant:
                    value = ((ConstantExpression)node).Value;
                    return true;
                case ExpressionType.MemberAccess:
                    var me = (MemberExpression)node;
                    object target;
                    if (TryGetLiteral(me.Expression, out target))
                    {
                        switch (me.Member.MemberType)
                        {
                            case System.Reflection.MemberTypes.Field:
                                value = ((FieldInfo)me.Member).GetValue(target);
                                return true;
                            case MemberTypes.Property:
                                value = ((PropertyInfo)me.Member).GetValue(target, null);
                                return true;
                        }
                    }
                    break;

            }
            return false;
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM