Custom equality comparer for Type in dictionary

纵然是瞬间 提交于 2019-12-02 02:25:38

问题


Since Int32 is a Object, I want this to print "True"

    Dictionary<Type, string> dict = new Dictionary<Type, string>(new MyComparer());
    dict[typeof(object)] = "Hello";

    Console.WriteLine(dict.ContainsKey(typeof(int))); // currently prints false :(

Here's the comparer I tried:

    public class MyComparer : IEqualityComparer<Type>
    {
        public bool Equals(Type x, Type y)
        {
            return y.IsAssignableFrom(x);
        }

        public int GetHashCode(Type obj)
        {
            return obj.GetHashCode();
        }
    }

But it's not working. I'm not quite sure what to return in GetHashCode - I know it's wrong cause when debugging I'm not even reaching Equals - Any idea how to write this correctly? Thanks.


回答1:


That simply is not a valid comparer for a dictionary, and the result is not well-defined. Equality comparisons should be commutative, specifically a eq b if and only if b eq a. That does not apply in your case. Likewise, a valid hash-code implementation states that:

  • if two hashes are non-equal, the two values are non-equal
  • two equal values must have the same hash-code

That fails too.

Basically, that isn't going to work.

Specifically, from MSDN:

Notes to Implementers

Implementations are required to ensure that if the Equals method returns true for two objects x and y, then the value returned by the GetHashCode method for x must equal the value returned for y.

The Equals method is reflexive, symmetric, and transitive. That is, it returns true if used to compare an object with itself; true for two objects x and y if it is true for y and x; and true for two objects x and z if it is true for x and y and also true for y and z.



来源:https://stackoverflow.com/questions/25808689/custom-equality-comparer-for-type-in-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!