[英]How do i convert a List<Interface> to List<Class> in c#
I have an interface defined as 我有一个定义为的接口
public interface IReaderInfo
{
string Displayname {get;}
}
and a class that implements that interface 以及实现该接口的类
public class ReaderInfo : IReaderInfo
{
string DisplayName {get;}
}
I then created a function which return List 然后我创建了一个返回List的函数
public List<ReaderInfo> GetReaders
{
var readers = new List<ReaderInfo>();
var Ireaders = someobject.Getreaders();// Returns the list of IReaderInfo.
// Now i would like cast Ireaders as readers and return.
}
How do i cast it? 我该如何施展它?
You have to create a new list with casted items: 您必须使用已铸造的项目创建新列表:
var readers = Ireaders.Cast<ReaderInfo>().ToList();
Or, if there is a possibility to have incompatible IReaderInfo entries and you only want the actual ReaderInfo objects in the result: 或者,如果有可能存在不兼容的IReaderInfo条目,并且您只想在结果中使用实际的ReaderInfo对象:
var readers = Ireaders.OfType<ReaderInfo>().ToList();
The quick&very-dirty fix is to pass only the objects that actually are ReaderInfo, by using OfType<> . 快速而非常脏的修复是通过使用OfType <>仅传递实际上是 ReaderInfo的对象。 Using Cast<> will fail if any of the objects implements
IReaderInfo
without actually being a ReaderInfo 如果任何对象实现
IReaderInfo
而实际上不是 ReaderInfo,则使用Cast <>将失败
var Ireaders = someobject.Getreaders();
var readers=Ireaders.OfType<ReaderInfo>().ToList();
return readers;
A better solution though would be to change the method's signature to return a List<IReaderInfo>
, or the more abstract IList<IReaderInfo>
. 但更好的解决方案是更改方法的签名以返回
List<IReaderInfo>
,或更抽象的IList<IReaderInfo>
。 If GetReaders
returns a list of interfaces it means it can return any object that implements the interface. 如果
GetReaders
返回接口列表,则意味着它可以返回实现该接口的任何对象。
Why discard some of them? 为什么要丢弃其中一些 ? Why should
GetReaders
decide which objects to use and which to discard when all implement the same interface? 为什么
GetReaders
应该决定使用哪些对象以及当所有对象都实现相同的接口时丢弃哪些对象?
public IList<IReaderInfo> GetReaders
{
var readers = new List<ReaderInfo>();
var Ireaders = someobject.Getreaders();// Returns the list of IReaderInfo.
return Ireaders;
}
For example, methods typically return interfaces when they do intend to return different types, eg mock objects, or handle multiple implementations. 例如,方法通常返回时, 他们打算返回不同的类型,例如模拟对象,或处理多个实现的接口。 By returning
List<ReaderInfo>
you prevent your method from handling valid objects 通过返回
List<ReaderInfo>
可以防止方法处理有效对象
UPDATE UPDATE
Versioning is another very strong reason why one shouldn't cast interfaces to concrete types. 版本控制是另一个非常强大的原因,为什么不应该将接口转换为具体类型。
Googling for IReaderInfo I found a couple of pages that refer to RFID readers. 谷歌搜索IReaderInfo我发现有几页涉及RFID阅读器。 A ReaderInfo returns the capabilities of a reader, but how do you expose new features for a new reader?
ReaderInfo返回阅读器的功能,但是如何为新阅读器公开新功能?
A common way to expose new features without breaking compatibility is to introduce a new class that implement the old interface IReaderInfo
and place expose new features through a new interface, eg IReaderInfo2
. 在不破坏兼容性的情况下公开新功能的常用方法是引入一个实现旧接口
IReaderInfo
的新类,并通过新接口(例如IReaderInfo2
公开新功能。 Or the existing class can be extended with IReaderInfo2
. 或者可以使用
IReaderInfo2
扩展现有类。 A library can do that because clients are expected to work only through the interfaces, not directly against the concrete classes. 库可以这样做,因为客户端只能通过接口工作,而不是直接针对具体类。
使用LINQ:
Ireaders.Cast<ReaderInfo>().ToList();
通过使用Linq
var derivedList = Ireaders.Cast<ReaderInfo>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.