从MSDN,代码分析警告CA1032:
Exception types must implement the following constructors:
我不明白为什么编译器无法解决在这里使用的正确重载.(下面的代码)只有一个版本的Add()是合适的--BigFoo是一个IFoo,并且没有实现IEnumerable,其中T是一个IFoo.但它坚持要报告歧义.有任何想法吗?我尝试添加第二个泛型类型参数 - 添加其中T:IFoo其中U:IEnumerable.但即使合法使用,过载也完全被忽略.
我知道我可以通过强制转换和指定泛型类型参数解决这个问题,但那时我已经打败了过载的目的.你可以质疑重载,但语义对我来说是正确的 - 我在我的类中实现的行为是Add()将对象批量添加为集合中的单个条目.(第二个Add()不应该是AddRange().)
namespace NS
{
interface IFoo { }
class BigFoo : IFoo, IEnumerable<int>
{
public IEnumerator<int> GetEnumerator()
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
class FooContainer
{
public void Add(IFoo item) { }
public void Add<T>(IEnumerable<T> group) where T : IFoo { }
}
class DemoClass
{
void DemoMethod()
{
BigFoo bigFoo = new BigFoo();
FooContainer fooContainer = new FooContainer();
// error CS0121: The call is ambiguous between the …Run Code Online (Sandbox Code Playgroud)