0

我有以下方法来比较 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();
}

我可以使用(a并且b是按照惯例共享相同属性但不是相同对象的对象的实例)来调用它:

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

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

但是,我发现了一种情况,我需要比较具有复杂类型属性的对象,并且我想比较复杂类型而不是对象的属性。像这样的东西:

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

我意识到我需要离开舒适的反射比较并进入表达式领域,但这里的主要任务是能够通过复杂类型的属性来表达属性访问器和属性访问器,这就是我迷路的地方。

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

4

1 回答 1

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);
    

好主意是添加一些生成方法的缓存,因为编译过程很昂贵。

于 2012-10-02T12:53:51.240 回答