转换失败:IList到实现 ICollection的自定义类

发布于 2024-09-19 00:56:31 字数 505 浏览 9 评论 0原文

这是我的自定义收集声明。

public interface IMenuCollection<T> : ICollection<T>
public class HTMLMenuCollection: IMenuCollection<HTMLMenu>

我正在尝试从另一个集合 IList 转换为它。

IList<HTMLMenu> htmlMenuList = new List<HTMLMenu>();
...
HTMLMenuCollection tempColl = (HTMLMenuCollection)htmlMenuList;

我不知道为什么这行不通。如果我将 IList 转换为 ICollection 它工作得很好,但这样我会得到一个无效的转换异常。我做错了什么?

This is my custom collection declaration.

public interface IMenuCollection<T> : ICollection<T>
public class HTMLMenuCollection: IMenuCollection<HTMLMenu>

I am trying to cast to it from another collection IList<T>.

IList<HTMLMenu> htmlMenuList = new List<HTMLMenu>();
...
HTMLMenuCollection tempColl = (HTMLMenuCollection)htmlMenuList;

I have no idea why this won't work. If I cast IList<T> to ICollection<T> it works just fine, but with this I get an invalid cast exception. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

神经大条 2024-09-26 01:00:34

另一种解决方法是使用通过实现 ICollection 提供给 HtmlMenuCollectionAddRange(IEnumerable) 方法>。

Another work-around could be to use the AddRange(IEnumerable<HtmlMenu>) method that is provided to HtmlMenuCollection through the implementation of ICollection<HtmlMenu>.

似梦非梦 2024-09-26 01:00:16

.net 中的转换是在运行时完成的,因此您必须查看实例的类型而不是变量的声明类型。 htmlMenuList 实例的类型为 List,无法转换为 HTMLMenuCollection,因为 List不是,也不从 HtmlMenuCollection 继承。

Casting in .net is done at run-time, so you must look at the type of the instance instead of the declared type of the variable. The type of htmlMenuList instance is List<HTMLMenu>, which cannot be casted to HTMLMenuCollection because List<HtmlMenu> is not, and does not inherited from HtmlMenuCollection.

好久不见√ 2024-09-26 00:59:52

HTMLMenuCollection 未实现IListICollection 强制转换有效,因为它们都继承自 ICollection

此外,您从 List 向上转换为 HTMLMenuCollection,这将不起作用,因为 HTMLMenuCollection 不是从 List< 继承的/code>,并且无论如何上转换都无法工作,因为编译器会认为 HTMLMenuCollectionList 拥有更多数据(即使没有)。

将列表传递到构造函数中可能更有用,并且将 ICollection 方法的内部实现传递到您在内部存储它的任何容器字段。

HTMLMenuCollection does not implement IList. The ICollection cast works because they both inherit from ICollection.

Also, you're upcasting from a List to an HTMLMenuCollection, which won't work because HTMLMenuCollection is not inheriting from List, and regardless the upconvert can't work since the compiler will consider HTMLMenuCollection to have more data (even if it doesn't) than a List.

It might be more useful to pass the list into a constructor, and have the internal implementations of the ICollection methods just pass to whatever container field you're storing it in internally.

绅刃 2024-09-26 00:59:23

它不会进行强制转换,因为 List 根本没有实现 HTMLMenuCollection,即使它具有相同的成员集也是如此。

It doesn't cast because List<HTMLMenu> simply does not implement HTMLMenuCollection, even if it has the same set of members.

屌丝范 2024-09-26 00:58:55

将继承视为一种“是”关系。

那么你可以认为它是因为 HTMLMenuCollection 是一个 List 但不是每个 List 都是一个 HTMLMenuCollection (因为 List 不是从 HTMLMenuCollection 继承的)。

您可以从 List 转换为 IList 再转换为 ICollection 的原因如下

public class List<T> : IList<T>, ICollection<T> // and more

public interface IList<T> : ICollection<T> // and more

Think of inheritance as a 'is a' relationship.

Then you can think of it as being because HTMLMenuCollection is a List<HTMLMenu> but not every List<HTMLMenu> is a HTMLMenuCollection (since List<HTMLMenu> doesn't inherit from HTMLMenuCollection).

The reason that you can cast from a List to IList to ICollection is the following:

public class List<T> : IList<T>, ICollection<T> // and more

and

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