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.