C# 将 List<T> 转换为 List<Interface>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8925400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cast List<T> to List<Interface>
提问by Andrew Kalashnikov
public interface IDic
{
int Id { get; set; }
string Name { get; set; }
}
public class Client : IDic
{
}
How can I cast List<Client>to List<IDic>?
我怎样才能投射List<Client>到List<IDic>?
采纳答案by Jon Skeet
You can't castit (preserving reference identity) - that would be unsafe. For example:
你不能投射它(保留参考标识)——那是不安全的。例如:
public interface IFruit {}
public class Apple : IFruit {}
public class Banana : IFruit {}
...
List<Apple> apples = new List<Apple>();
List<IFruit> fruit = apples; // Fortunately not allowed
fruit.Add(new Banana());
// Eek - it's a banana!
Apple apple = apples[0];
Now you can convert a List<Apple>to an IEnumerable<IFruit>in .NET 4 / C# 4 due to covariance, but if you want a List<IFruit>you'd have to create a newlist. For example:
现在,由于协方差,您可以在 .NET 4 / C# 4 中将a 转换List<Apple>为 an IEnumerable<IFruit>,但如果您想要 a List<IFruit>,则必须创建一个新列表。例如:
// In .NET 4, using the covariance of IEnumerable<T>
List<IFruit> fruit = apples.ToList<IFruit>();
// In .NET 3.5
List<IFruit> fruit = apples.Cast<IFruit>().ToList();
But this is notthe same as casting the original list - because now there are two separatelists. This is safe, but you need to understand that changes made to one list won'tbe seen in the other list. (Modifications to the objectsthat the lists refer to will be seen, of course.)
但是,这是不一样的铸造原名单-因为现在有两个单独的列表。这是安全的,但您需要了解对一个列表所做的更改不会在另一个列表中看到。(当然,会看到对列表所指对象的修改。)
回答by Piotr Auguscik
Its only possible by creating new List<IDic>and transfering all elements.
它只能通过创建新List<IDic>元素并转移所有元素来实现。
回答by Andras Zoltan
A Cast iterator and .ToList():
一个 Cast 迭代器和 .ToList():
List<IDic> casted = input.Cast<IDic>().ToList()will do the trick.
List<IDic> casted = input.Cast<IDic>().ToList()会做的伎俩。
Originally I said covariance would work - but as Jon has rightly pointed out; no it won't!
最初我说协方差会起作用 - 但正如乔恩正确指出的那样;不,不会!
And originally I also stupidly left off the ToList()call
而原本我也傻傻的ToList()挂断了电话
回答by musefan
If you can use LINQ then you can do this...
如果您可以使用 LINQ,那么您可以执行此操作...
List<Client> clientList = new List<Client>();
List<IDic> list = clientList.Select(c => (IDic)c).ToList();
回答by Marc Messing
List<Client> listOfA = new List<Client>();
List<IDic> list = listOfA.Cast<IDic>().ToList();
回答by ?iamond ?eeze?
I too had this problem and after reading Jon Skeet's answer I modified my code from using List<T>to use IEnumerable<T>. Although this does not answer the OP's original question of How can I cast List<Client>to List<IDic>, it does avoid the need to do so and thus may be helpful to others who encounter this issue. This of course assumes that the code that requires the use of List<IDic>is under your control.
我也有这个问题,在阅读了 Jon Skeet 的回答后,我将代码从 using 修改List<T>为 use IEnumerable<T>。虽然这不能回答 OP 的原始问题How can I cast List<Client>toList<IDic>,但它确实避免了这样做的需要,因此可能对遇到此问题的其他人有所帮助。这当然假设需要使用的代码List<IDic>在您的控制之下。
E.g.:
例如:
public void ProcessIDic(IEnumerable<IDic> sequence)
{
// Implementation
}
Instead of:
代替:
public void ProcessIDic(List<IDic> list)
{
// Implementation
}
回答by Kent Aguilar
In .Net 3.5, you can do the following:
在 .Net 3.5 中,您可以执行以下操作:
List<ISomeInterface> interfaceList = new List<ISomeInterface>(list.Cast<ISomeInterface>());
The constructor for List in this case takes an IEnumerable.
listthough is only convertible to IEnumerable. Even though myObjmay be convertible to ISomeInterface the type IEnumerable is not convertible to IEnumerable.
在这种情况下,List 的构造函数采用 IEnumerable。
list虽然只能转换为 IEnumerable。即使myObj可以转换为 ISomeInterface,IEnumerable 类型也不能转换为 IEnumerable。

