如何解决 Generic.IList.this[] 和 IList.this[] 之间的调用歧义?

发布于 2024-07-04 07:23:33 字数 891 浏览 13 评论 0原文

我有一个集合,它实现了一个扩展 IList和 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实现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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

故事与诗 2024-07-11 07:23:33

不幸的是,您不能声明具有相同参数列表的两个索引器。 以下段落摘自此处 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:

The signature of an indexer consists of the number and types of its formal parameters. It does not include the indexer type or the names of the formal parameters. If you declare more than one indexer in the same class, they must have different signatures.

You will have to declare a different set of parameters if you wish to use the second indexer.

浮萍、无处依 2024-07-11 07:23:33

将您的通用实现更改为...

T IList<T>.this[int index] { get; set; }

这明确说明了哪个“这个”是哪个。

Change your generic implementation to...

T IList<T>.this[int index] { get; set; }

This explicitly says which 'this' is which.

衣神在巴黎 2024-07-11 07:23:33

这是我的问题的骗局

总结,如果你这样做,它就解决了问题:

public Interface IMySpecialCollection : IList<MyObject>, IList
{
    new MyObject this[int index];
    ... 
}

This is a dupe of my question here

To summarise, if you do this, it solves the problem:

public Interface IMySpecialCollection : IList<MyObject>, IList
{
    new MyObject this[int index];
    ... 
}
后知后觉 2024-07-11 07:23:33

您不能使用

public interface IMySpecialCollection : IList, IList { ... } 执行此操作,

但是您可以使用类执行您想要的操作,您需要实现以下之一接口明确。 在我的示例中,我明确了 IList。

public class MySpecialCollection : IList, IList { ... }

IList; myspecialcollection = new MySpecialCollection();
IList list = (IList)myspecialcollection;

您是否考虑过让 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.

靖瑶 2024-07-11 07:23:33

列表暗示了 IList,所以在同一个类中使用两者是一个坏主意。

List<T> implies IList, so it's a bad idea to use both in the same class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文