我有 2 个课程:\nParent和Child:Parent.
当我下一步时:
\n\nIMyRepository<Child> _childRepository=new MyRepository<Child>();\nIMyRepository<Parent> _repository=childRepository;\nRun Code Online (Sandbox Code Playgroud)\n\n我收到错误“\xd0\xa1an\t 将源类型转换为目标类型”。\n请告诉我为什么此代码不起作用。
\n因为,那么您将能够插入new AnotherDifferentChild()\xe2\x80\x93 ,这不可能存在于IList<Child>. 如果您想了解更多详细信息,请查找有关协方差、逆变和不变性的文章。
如果要创建一个新列表,保存类型的引用Parent,可以使用Cast<T>()LINQ 中的方法:
IList<Parent> parentList = childList.Cast<Parent>().ToList();\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
501 次 |
| 最近记录: |