Suppose I have some IEqualityComparer<in T>
implemented. I have some constraints in my framework and I am using the microsoft MVVM framework. So the moment I am calling the functionality, my T
will be of type object
, while I know that the underlying values are in this case double
.
I receive an InvalidCastException:
Unhandled exception. System.InvalidCastException: Unable to cast object of type 'DoubleEqualityComparer' to type 'System.Collections.Generic.IEqualityComparer`1[System.Object]'
I tried the same with a class Foo
, but also no luck with the cast. I expected that because T
is only in
, that it would resolve well. What am I missing?
using System.Collections.Generic;
public class DoubleEqualityComparer : IEqualityComparer<double>
{
public bool Equals(double a, double b)
{
return true;
}
public int GetHashCode(double a)
{
return a.GetHashCode();
}
}
public class Program
{
public static void Main()
{
var comparer = new DoubleEqualityComparer();
var objectComparer = (IEqualityComparer<object>)comparer;
}
}