I'm struggling with implementing the IEquatable<>
interface for a class. The class has a Parameter
property that uses a generic type. Basically the class definition is like this:
public class MyClass<T> : IEquatable<MyClass<T>>
{
public T Parameter { get; }
...
}
In the Equals()
method I'm using EqualityComparer<T>.Default.Equals(Parameter, other.Parameter)
to compare the property. Generally, this works fine – as long as the property is not a collection, for example an IEnumerable<T>
. The problem is that the default equality comparer for IEnumerable<T>
is checking reference equality.
Obviously, you'd want to use SequenceEqual()
to compare the IEnumerable<T>
. But to get this running, you need to specify the generic type of the SequenceEqual()
method. This is the closest I could get:
var parameterType = typeof(T);
var enumerableType = parameterType.GetInterfaces()
.Where(type => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
.Select(type => type.GetGenericArguments().First()).FirstOrDefault();
if (enumerableType != null)
{
var castedThis = Convert.ChangeType(Parameter, enumerableType);
var castedOther = Convert.ChangeType(other.Parameter, enumerableType);
var isEqual = castedThis.SequenceEqual(castedOther);
}
But this does not work because Convert.ChangeType()
returns an object
. And of course object
does not implement SequenceEqual()
.
How do I get this working? Thanks for any tipps!
Best regards, Oliver