使用表达式比较属性和子属性上的对象

时间:2012-10-02 12:15:10

标签: c# expression-trees expression

我有以下方法来比较DTO。

bool Equals<T1, T2>(T1 t1, T2 t2, params Expression<Func<T1, object>>[] accessors)
{
  return !(
    from accessor in accessors 
    select ((MemberExpression) accessor.Body).Member.Name into propertyName 
    let p1 = typeof (T1).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly) 
    let p2 = typeof (T2).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly) 
    let p1val = p1.GetValue(t1, null) 
    let p2val = p2.GetValue(t2, null) 
    where !Equals(p1val, p2val) 
    select p1val
  ).Any();
}

我可以使用(ab来调用它是按惯例共享相同属性但不是相同对象的对象实例:

Equals(a, b, x => x.PropertyOne, x => x.PropertyTwo );

比较属性的对象属性,这在大多数情况下都很好。

但是,我发现了一个案例,我需要比较具有复杂类型属性的对象,以及我想要比较复杂类型的属性而不是对象的属性。像这样:

Equals(a, b, x => x.ComplexTypeProperty.ChildProp );

我已经意识到我需要离开舒适的反射比较并输入Expression land,但这里的主要任务是能够通过复合体表达属性访问器和属性访问器输入属性,这就是我迷失的地方。

任何指针都会很好,谢谢!

1 个答案:

答案 0 :(得分:1)

任务并不复杂:

  1. 确定表达式给出的属性路径或表达式。例如,这种扩展方法将为您提供:

    public static IEnumerable<string> GetPropertiesNames<T, G>(this Expression<Func<T, G>> pathExpression)
    {
        List<string> _propertyNames = new List<string>();
    
        Expression expression = pathExpression.Body;
    
        if (expression.NodeType == ExpressionType.Convert)
        {
            var convert = (UnaryExpression)pathExpression.Body;
            expression = convert.Operand;
        }
    
        while (expression.NodeType == ExpressionType.MemberAccess)
        {
            MemberExpression memberExpression = (MemberExpression)expression;
              if(!(memberExpression.Member is PropertyInfo)) 
                    throw new InvalidOperationException();
            _propertyNames.Add(memberExpression.Member.Name);
            expression = memberExpression.Expression;
        }
    
        if (expression.NodeType != ExpressionType.Parameter)
            throw new InvalidOperationException();
    
        return _propertyNames;
    }
    
  2. 聚合第二种类型的表达式以创建将返回值的函数:

    var parameter = Expression.Parameter(typeof(T2));      
    var expressionToConvert =  accessors[0]; //for future loop
    
        var propertyChainDescriptor = expressionToConvert.GetPropertiesNames() 
             .Aggregate(new { Expression = (Expression)parameterCasted, Type = typeof(T2)},
                 (current, propertyName) =>
                 {
                     var property = current.Type.GetProperty(propertyName);
                     var expression = Expression.Property(current.Expression, property);
                     return new { Expression = (Expression)expression, Type = property.PropertyType };
                 });
    
        var body = propertyChainDescriptor.Expression;
    
        if (propertyChainDescriptor.Type.IsValueType)
        {
            body = Expression.Convert(body, typeof(object));
        }
    
        var t2PropertyRetriver = Expression.Lambda<Func<T2, object>>(body, parameter).Compile();
    
  3. 现在执行检索值并进行比较的方法:

        var t1PropertyRetriver = accessor[0].Compile();
        var t1Value = t1PropertyRetriver(t1);
        var t2Value = t2PropertyRetriver(t2);
    
        var areEqual = object.Equals(t1Value,t2Value);
    
  4. 好主意是添加一些生成方法的缓存,因为编译过程很昂贵。