Sib*_*Guy 12 .net c# extension-methods overloading fluent-assertions
我有两种扩展方法:
public static IPropertyAssertions<T> ShouldHave<T>(this T subject)
{
return new PropertyAssertions<T>(subject);
}
public static IPropertyAssertions<T> ShouldHave<T>(this IEnumerable<T> subject)
{
return new CollectionPropertyAssertions<T>(subject);
}
Run Code Online (Sandbox Code Playgroud)
现在我写一些使用它的代码:
List<Customer> collection2 = new List<Customer>();
collection2.ShouldHave(); //first overload is chosen
IEnumerable<Customer> collection3 = new List<Customer>();
collection3.ShouldHave(); //second overload is chosen
Run Code Online (Sandbox Code Playgroud)
仅当我明确指定IEnumerable类型时才选择第二个重载.有没有办法在两种情况下都选择第二次过载?
第一个重载是更好的匹配,因为T被推断为List<Customer>,这给出了完全匹配.对于第二个重载,它会将T推断为Customer,因此参数将是IEnumerable<Customer>,这是一个不太精确的匹配List<Customer>.
不要这么认为.不要认为在这种情况下IEnumerable<T>可能总是调用重载,因为它在类型方面的匹配不太准确T.如果未指定具体IEnumerable<T>,则在这种情况下,最佳匹配将始终是第一种方法.
在 Fluent Assertion 2.0 中,我终于设法以体面的方式解决了上述问题。在这里阅读所有相关内容: http://www.dennisdoomen.net/2012/09/asserting-object-graph-equivalence.html
| 归档时间: |
|
| 查看次数: |
598 次 |
| 最近记录: |