实现IEqualityComparer <t>以比较任何类(包括匿名)的任意属性</t>

时间:2012-07-05 13:56:11

标签: c# generics reflection equality iequalitycomparer

我正在编写实现IEqualityComparer的这个整洁的类,因此我可以将任何匿名类型传递给它(或实际上任何具有属性的类型),它将通过比较类型的属性值自动比较类型。

public class CompareProperty<T> : IEqualityComparer<T>
    {
        private Type type;
        private PropertyInfo propInfo;
        private string _fieldName;

        public string fieldName
        {
            get;
            set;
        }

        public CompareProperty(string fieldName)
        {
            this.fieldName = fieldName;
        }

        public bool Equals<T>(T x, T y)
        {
            if (this.type == null)
            {
                type = x.GetType();
                propInfo = type.GetProperty(fieldName);
            }
            object objX = propInfo.GetValue(x, null);
            object objY = propInfo.GetValue(y, null);
            return objX.ToString() == objY.ToString();
        }
    }

我认为这是一个很好的小助手功能,我可以多次使用。

为了使用它,我必须这样做:

var t = typeof(CompareProperty<>);
var g = t.MakeGenericType(infoType.GetType());
var c = g.GetConstructor(new Type[] {String.Empty.GetType()});
var obj = c.Invoke(new object[] {"somePropertyName"});

足够公平,但我如何处理它返回的obj变量?

someEnumerable.Distinct(obj);

distinct扩展函数的重载不接受这个,因为它没有看到IEqualityComparer类型,当然它只看到一个对象。

someEnumerable.Distinct((t) obj);
someEnumerable.Distinct(obj as t);

这也行不通。未找到类型/名称空间(红色下划线)。

我如何做到这一点?

2 个答案:

答案 0 :(得分:6)

我将首先为非匿名类型提供解决方案,然后将其扩展为匿名类型。希望它能帮助您理解人们在评论中对您的问题所说的内容。

我的“通用”IEqualityComparer<>如下所示:

public class PropertyComparer<T> : IEqualityComparer<T>
{
    private readonly PropertyInfo propertyToCompare;

    public PropertyComparer(string propertyName)
    {
        propertyToCompare = typeof(T).GetProperty(propertyName);
    }
    public bool Equals(T x, T y)
    {
        object xValue = propertyToCompare.GetValue(x, null);
        object yValue = propertyToCompare.GetValue(y, null);
        return xValue.Equals(yValue);
    }

    public int GetHashCode(T obj)
    {
        object objValue = propertyToCompare.GetValue(obj, null);
        return objValue.GetHashCode();
    }
}

假设我们首先要使用非匿名类型,例如Person

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string Surname { get; set; }
 }

用法非常简单:

IEnumerable<Person> people = ... ; // some database call here
var distinctPeople = people.Distinct(new PropertyComparer<Person>("FirstName"));

如您所见,要使用PropertyComparer<T>,我们需要指定要相互比较的类型(T)实例。处理匿名类型时T会是什么?由于它们是在运行时生成的,因此您不能通过直接创建其实例来使用比较器,因为您在编译时不知道T。相反,您需要使用type-inference让C#编译器自己从上下文推断出T。这样做的好方法是编写以下扩展方法:

public static class LinqExtensions
{
    public static IEnumerable<T> WithDistinctProperty<T>(this IEnumerable<T> source, string propertyName)
    {
        return source.Distinct(new PropertyComparer<T>(propertyName));
    }
}

现在它也可以使用匿名类型:

var distinctPeople = people
        .Select(x => new { x.FirstName, x.Surname })
        .WithDistinctProperty("FirstName");

突然之间,没有必要指定查询在任何地方处理的确切类型,因为C#编译器足够聪明,可以从上下文中推断出来(在这种情况下,是从类型中提供的)扩展方法中的source参数)。

希望这会对你有所帮助。

答案 1 :(得分:1)

只需添加自己的验证。

class PropertyComparer<T, TResult> : IEqualityComparer<T>
{
    private Func<T, TResult> _getProperty;

    public PropertyComparer(Func<T, TResult> predicate)
    {
        this._getProperty = predicate;
    }

    public bool Equals(T x, T y)
    {
        return this._getProperty(x).Equals(_getProperty(y));
    }

    public int GetHashCode(T obj)
    {
        return this._getProperty(obj).GetHashCode();
    }
}

带扩展方法的类:

public static class IEnumerableExtensions
{
    public static IEnumerable<TSource> DistinctBy<TSource, TResult>
        (this IEnumerable<TSource> source, Func<TSource, TResult> predicate)
    {
        return source.Distinct(new PropertyComparer<TSource, TResult>(predicate));
    }
}

用法:

someEnumerable.DistinctBy(i => i.SomeProperty);
相关问题