Rom*_*ter 11 c# extension-methods type-inference type-resolution
假设我已经定义了两个不相关的类型和两个具有相同签名但不同类型过滤器的扩展方法:
public class Foo {}
public class Bar {}
public static class FooExtensions
{
public static TFoo Frob<TFoo>(this TFoo foo) where TFoo : Foo { }
public static TFoo Brob<TFoo>(this TFoo foo) where TFoo : Foo { }
}
public static class BarExtensions
{
public static TBar Frob<TBar>(this TBar bar) where TBar : Bar { }
}
Run Code Online (Sandbox Code Playgroud)
然后,当我写作时,new Foo().Frob();我得到一个错误
error CS0121: The call is ambiguous between the following methods or properties: 'FooExtensions.Frob<TFoo>(TFoo)' and 'BarExtensions.Frob<TBar>(TBar)'
有人可以解释为什么这会失败以及如何避免它?
编辑:这发生在VS2015 Update 3和VS2017 RC中.
EDIT2:这里的想法是拥有适用于类层次结构的流畅API:
new Foo()
.Frob()
.Brob()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
542 次 |
| 最近记录: |