如何解决 Generic.IList.this[] 和 IList.this[] 之间的调用歧义?
我有一个集合,它实现了一个扩展 IList
public Interface IMySpecialCollection : IList<MyObject>, IList { ... }
这意味着我有两个版本的索引器。
我希望使用通用实现,因此我通常实现该实现:
public MyObject this[int index] { .... }
我只需要 IList 版本进行序列化,因此我显式实现它,以将其隐藏:
object IList.this[int index] { ... }
但是,在我的单元测试中,以下
MyObject foo = target[0];
结果会导致编译器错误
调用之间不明确 以下方法或属性
我对此有点惊讶; 我相信我以前做过并且效果很好。 我在这里缺少什么? 我怎样才能获得 IList
编辑 IList
再次编辑:我必须从界面中删除 IList 并将其移到我的班级上。 我不想这样做,因为实现该接口的类最终将被序列化为 Xaml,这需要集合来实现 IDictionary 或 IList...
I've got a collection that implements an interface that extends both IList<T> and List.
public Interface IMySpecialCollection : IList<MyObject>, IList { ... }
That means I have two versions of the indexer.
I wish the generic implementation to be used, so I implement that one normally:
public MyObject this[int index] { .... }
I only need the IList version for serialization, so I implement it explicitly, to keep it hidden:
object IList.this[int index] { ... }
However, in my unit tests, the following
MyObject foo = target[0];
results in a compiler error
The call is ambiguous between the
following methods or properties
I'm a bit surprised at this; I believe I've done it before and it works fine. What am I missing here? How can I get IList<T> and IList to coexist within the same interface?
Edit IList<T> does not implement IList, and I must implement IList for serialization. I'm not interested in workarounds, I want to know what I'm missing.
Edit again: I've had to drop IList from the interface and move it on my class. I don't want to do this, as classes that implement the interface are eventually going to be serialized to Xaml, which requires collections to implement IDictionary or IList...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不幸的是,您不能声明具有相同参数列表的两个索引器。 以下段落摘自此处 C# 编程指南 - 使用索引器“备注”部分:
如果您希望使用第二个索引器,则必须声明一组不同的参数。
Unfortunately you can't declare two indexers with the same parameter list. The following paragraph is taken from here C# Programming Guide - Using Indexers "Remarks" section:
You will have to declare a different set of parameters if you wish to use the second indexer.
将您的通用实现更改为...
这明确说明了哪个“这个”是哪个。
Change your generic implementation to...
This explicitly says which 'this' is which.
这是我的问题的骗局
总结,如果你这样做,它就解决了问题:
This is a dupe of my question here
To summarise, if you do this, it solves the problem:
您不能使用
public interface IMySpecialCollection : IList, IList { ... } 执行此操作,
但是您可以使用类执行您想要的操作,您需要实现以下之一接口明确。 在我的示例中,我明确了 IList。
public class MySpecialCollection : IList, IList { ... }
IList
您是否考虑过让 IMySpecialCollection 实现 ISerializable 来进行序列化?
支持多种集合类型对我来说似乎有点错误。 您可能还想考虑将 IList 转换为 IEnumerable 进行序列化,因为 IList 仅包装 IEnumerable 和 ICollection。
You can't do this with
public interface IMySpecialCollection : IList<MyObject>, IList { ... }
But you can do what you want with a class, you will need to make the implementations for one of the interfaces explicit. In my example I made IList explicit.
public class MySpecialCollection : IList<MyObject>, IList { ... }
IList<object> myspecialcollection = new MySpecialCollection();
IList list = (IList)myspecialcollection;
Have you considered having IMySpecialCollection implement ISerializable for serialization?
Supporting multiple collection types seems a bit wrong to me. You may also want to look at casting yout IList to IEnumerable for serialization since IList just wraps IEnumerable and ICollection.
列表暗示了 IList,所以在同一个类中使用两者是一个坏主意。
List<T> implies IList, so it's a bad idea to use both in the same class.