如何转换IList< SomeObject>到IList< ISomeInterface>其中SomeObject在C#4.0中使用协方差来实现ISomeInterface [英] How to Convert IList<SomeObject> to IList<ISomeInterface> where SomeObject implements ISomeInterface using covariance in C# 4.0

查看:111
本文介绍了如何转换IList< SomeObject>到IList< ISomeInterface>其中SomeObject在C#4.0中使用协方差来实现ISomeInterface的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何转换IList到IList,其中SomeObject在C#4.0中使用协方差实现ISomeInterface

How to Convert IList to IList where SomeObject implements ISomeInterface using covariance in C# 4.0

我有类似于以下的东西

IList<Items> GetItems;

IList<IItems> items = GetItems() as IList<IItems>; 

但项目为null;

这里的答案是4.0之前的:

the answer here was for pre 4.0:

将类型T的数组转换为类型I的数组,其中T在C#中实现I。 / p>

Converting an array of type T to an array of type I where T implements I in C#.

推荐答案

为什么不直接使用

IList<Items> GetItems;
IList<IItems> items = GetItems().Cast<IItems>().ToList(); 

这篇关于如何转换IList&lt; SomeObject&gt;到IList&lt; ISomeInterface&gt;其中SomeObject在C#4.0中使用协方差来实现ISomeInterface的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆