开发者

Generic PropertyEqualityComparer<T>

开发者 https://www.devze.com 2023-02-12 20:28 出处:网络
I\'ve developed a generic PropertyEqualityComparer that works fine, but I\'m not sure I\'ve done it in the right way, so if some of who c开发者_Python百科an improve the code, or critic, he\'s welcome.

I've developed a generic PropertyEqualityComparer that works fine, but I'm not sure I've done it in the right way, so if some of who c开发者_Python百科an improve the code, or critic, he's welcome.

Note : Property should implement IEquatable if is a Reference type.

    public sealed class PropertyEqualityComparer<T> : IEqualityComparer<T>
{
    private readonly Type _iequatable = Type.GetType("System.IEquatable`1", false, true);
    private readonly PropertyInfo _property;

    public PropertyEqualityComparer(string property)
    {
        PropertyInfo propInfos = typeof(T).GetProperty(property);
        if (propInfos == null)
        {
            throw new ArgumentNullException();
        }

        // Ensure Property is Equatable (override of HashCode)
        if (propInfos.PropertyType.IsValueType
            || (!propInfos.PropertyType.IsValueType
                && propInfos.PropertyType.GetInterfaces().Any(type => type.Name == _iequatable.Name)))
        {
            _property = propInfos;
        }
        else
        {
            throw new ArgumentException();
        }
    }

    public bool Equals(T x, T y)
    {
        var xValue = _property.GetValue(x, null);
        var yValue = _property.GetValue(y, null);

        return xValue.Equals(yValue);
    }

    public int GetHashCode(T obj)
    {
        return _property.GetValue(obj, null).GetHashCode();
    }
}

I've created two classes to see if works and all is fine, here are the classes :

public sealed class A
{
    private string _s1;
    private B _b;

    public A(string s1, B b)
    {
        _s1 = s1;
        _b = b;
    }

    public string S1
    {
        get { return _s1; }
        set { _s1 = value; }
    }

    public B B
    {
        get { return _b; }
        set { _b = value; }
    }
}

public sealed class B : IEquatable<B>
{
    private string _s;

    public string S
    {
      get { return _s; }
      set { _s = value; }
    }

    public B(string s)
    {
        S = s;
    }

    public override int GetHashCode()
    {
        return S.GetHashCode();
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as B);
    }

    public bool Equals(B other)
    {
        return (other == null)
            ? false
            : this.S == other.S;
    }
}

And the test code :

B b = new B("baby");
A[] __a = { new A("first", b), new A("second", b), new A("third", b)};
PropertyEqualityComparer<A> aComparer = new PropertyEqualityComparer<A>("B");

var vDistinct = __a.Distinct(aComparer).ToArray();
// vDistinct = __a[0] { first, baby }
var vContains = __a.Contains(new A("a", new B("baby")), aComparer);
// True
vContains = __a.Contains(new A("b", new B("foobar")), aComparer);
// False

Is here something to improve?

Thanks !


Since the type of T must be known at compile-time there's no need to pass the property name as a string. You could pass a delegate to perform type-safe, fast member access instead of brittle, slow reflection:

var comparer = new ProjectionEqualityComparer<A, B>(a => a.B);

// ...

public sealed class ProjectionEqualityComparer<TSource, TKey>
    : EqualityComparer<TSource>
{
    private readonly Func<TSource, TKey> _keySelector;
    private readonly IEqualityComparer<TKey> _keyComparer;

    public ProjectionEqualityComparer(Func<TSource, TKey> keySelector,
        IEqualityComparer<TKey> keyComparer = null)
    {
        if (keySelector == null)
            throw new ArgumentNullException("keySelector");

        _keySelector = keySelector;
        _keyComparer = keyComparer ?? EqualityComparer<TKey>.Default;
    }

    public override bool Equals(TSource x, TSource y)
    {
        if (x == null)
            return (y == null);

        if (y == null)
            return false;

        return _keyComparer.Equals(_keySelector(x), _keySelector(y));
    }

    public override int GetHashCode(TSource obj)
    {
        if (obj == null)
           throw new ArgumentNullException("obj");

        return _keyComparer.GetHashCode(_keySelector(obj));
    }
}

And if you really need to enforce the IEquatable<T> rule then you can just add a where TKey : IEquatable<TKey> generic constraint, but it really shouldn't be necessary. The use of EqualityComparer<TKey>.Default should take care of all that for you, or you can pass in a custom IEqualityComparer<TKey> implementation if you prefer.


Looks pretty good. It is possible to speed up the reflection used to get property values. A good article on the subject here: http://www.codeproject.com/KB/cs/ReflectionDemo.aspx.

The question that will always be asked is "why" - do you need to be so generic?

If this is a piece of code that will be called a lot then you might consider using CodeDom to generate a custom implementation.

0

精彩评论

暂无评论...
验证码 换一张
取 消