转换失败:IList到实现 ICollection的自定义类
这是我的自定义收集声明。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
另一种解决方法是使用通过实现
ICollection
提供给HtmlMenuCollection
的AddRange(IEnumerable)
方法>。Another work-around could be to use the
AddRange(IEnumerable<HtmlMenu>)
method that is provided toHtmlMenuCollection
through the implementation ofICollection<HtmlMenu>
..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 isList<HTMLMenu>
, which cannot be casted toHTMLMenuCollection
becauseList<HtmlMenu>
is not, and does not inherited fromHtmlMenuCollection
.HTMLMenuCollection
未实现IList
。ICollection
强制转换有效,因为它们都继承自ICollection
。此外,您从
List
向上转换为HTMLMenuCollection
,这将不起作用,因为HTMLMenuCollection
不是从List< 继承的/code>,并且无论如何上转换都无法工作,因为编译器会认为
HTMLMenuCollection
比List
拥有更多数据(即使没有)。将列表传递到构造函数中可能更有用,并且将
ICollection
方法的内部实现传递到您在内部存储它的任何容器字段。HTMLMenuCollection
does not implementIList
. TheICollection
cast works because they both inherit fromICollection
.Also, you're upcasting from a
List
to anHTMLMenuCollection
, which won't work becauseHTMLMenuCollection
is not inheriting fromList
, and regardless the upconvert can't work since the compiler will considerHTMLMenuCollection
to have more data (even if it doesn't) than aList
.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.它不会进行强制转换,因为
List
根本没有实现HTMLMenuCollection
,即使它具有相同的成员集也是如此。It doesn't cast because
List<HTMLMenu>
simply does not implementHTMLMenuCollection
, even if it has the same set of members.将继承视为一种“是”关系。
那么你可以认为它是因为
HTMLMenuCollection
是一个List
但不是每个List
都是一个HTMLMenuCollection
(因为List
不是从HTMLMenuCollection
继承的)。您可以从 List 转换为 IList 再转换为 ICollection 的原因如下
:
Think of inheritance as a 'is a' relationship.
Then you can think of it as being because
HTMLMenuCollection
is aList<HTMLMenu>
but not everyList<HTMLMenu>
is aHTMLMenuCollection
(sinceList<HTMLMenu>
doesn't inherit fromHTMLMenuCollection
).The reason that you can cast from a List to IList to ICollection is the following:
and