[英]C# not choosing the right overload with a default value
I am using the FluentValidation framework in C# which has the following two method overloads: 我在C#中使用FluentValidation框架 ,它具有以下两个方法重载:
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. 基本上,其中一个重载允许您传入实际的TProperty进行比较,另一个允许您执行一个从TObject返回TProperty的lambda。
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? 有没有办法可以强迫C#使用第二个重载?
Edit: 编辑:
ObjectComparer is an IEqualityComparer: ObjectComparer是IEqualityComparer:
public class ObjectComparer : IEqualityComparer<MyClass>
{
// ...
}
IEqualityComparer<T>
does not inherit IEqualityComparer
and therefore your class does not implement IEqualityComparer
! IEqualityComparer<T>
不继承IEqualityComparer
,因此您的类不实现IEqualityComparer
! Implement both of them. 实施它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.