I am using the FluentValidation framework in C# which has the following two method overloads:
IRuleBuilder<TObject, TProperty>
IRuleBuilderOptions<TObject, TProperty> Equal(TProperty toCompare, [IEqualityComparer comparer = null])
IRuleBuilderOptions<TObject, TProperty> Equal(Expression<Func<TObject,TProperty>> expression, [IEqualityComparer comparer = null])
Basically one of the overloads lets you pass in an actual TProperty to compare, and the other lets you do a lambda that returns a TProperty from a TObject.
When I do the following it works:
RuleFor(r => r.First).Equals(r => r.Second);
It is getting the right overload in this case. When I try and pass in a value for the comparer it defaults to the other overload:
RuleFor(r => r.First).Equals(r => r.Second, new ObjectComparer()); // will not compile
This will not compile because it thinks I’m trying to use the first overload.
Is there a way I can force C# to use the second overload?
Edit:
ObjectComparer is an IEqualityComparer:
public class ObjectComparer : IEqualityComparer<MyClass>
{
// ...
}
IEqualityComparer<T>does not inheritIEqualityComparerand therefore your class does not implementIEqualityComparer! Implement both of them.