C#:从其基类的实例返回继承的类(通用列表)
这可能是我只是完全倒退地记住了事情,但我想更多地了解我做错了什么......
我已经声明一个类只不过是从通用列表直接继承(这样做是为了简化命名) ),像这样:
public class FooList : List<Foo> {}
现在在与此类完全分离的另一个方法中,我尝试返回此类的实例,但是我想根据标准过滤该类,因此我使用 lambda 表达式:
var list = new FooList(); // imagine this fills it with different items
var filtered = list.FindAll(c => c.Something == "filter criteria");
现在根据对于 FindAll 方法,这应该返回一个 List[Foo]。但是,我想将此对象作为 FooList 返回,而不是 List[Foo]。我是否必须创建 FooList 的新实例并从 List[Foo] 复制项目?
如果是这样,为什么?为什么我不能直接将 List 转换为 FooList,因为它们是同一个对象?
如果可以做到这一点,我该怎么做?
非常感谢!
This is probably me just remembering things completely backwards, but I'd like to know more about what I'm doing wrong...
I have declared a class to be nothing more than a direct inheritance from a generic list (done to simplify naming), something like this:
public class FooList : List<Foo> {}
now in another method completely separate from this class, I am trying to return an instance of this class, however I want to filter the class based on a criterion, so I'm using a lambda expression:
var list = new FooList(); // imagine this fills it with different items
var filtered = list.FindAll(c => c.Something == "filter criteria");
now according to the FindAll method, this SHOULD return a List[Foo]. However, I want to return this object as a FooList, not a List[Foo]. Do I have to create a new instance of FooList and copy the items from the List[Foo]?
If so, why? why can't I convert a List to a FooList directly, since they are the same object?
If this CAN be done, how do I do it?
many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题是您使用了错误的工具来完成这项工作。 继承不是一种简化命名的机制;继承是一种机制,主要设计用于 (1) 对业务域对象类之间的“一种”关系进行建模,以及 (2) 实现相关类型之间实现细节的共享和专门化。
如果您想简化命名,那么正确的工具是将其放在
每个文件的顶部。
Your problem is that you are using the wrong tool for the job. Inheritance is not a mechanism for simplifying naming; inheritance is a mechanism primarily designed for (1) modeling "is a kind of" relationships between classes of business domain objects, and (2) enabling the sharing and specialization of implementation details amongst related types.
If you want to simplify naming then the right tool for the job is to put
at the top of every file.
它们不是同一件事。
FooList
是一个List
,但也是一个List
,它由FindAll()
返回code> 函数继承自List
,不是FooList
。您需要构造一个新的FooList
。您可以执行类似为
FooList
创建一个构造函数的操作,该构造函数采用IEnumerable
对象,如下:或者您也可以执行以下操作:
所示 其他答案,如果您不添加任何功能,那么您应该使用
using
关键字创建一个别名。They are not the same thing. A
FooList
is aList<foo>
but aList<foo>
, which is being returned by theFindAll()
function inherited fromList<foo>
, is not aFooList
. You will need to construct a newFooList
.You could do something like create a Constructor for
FooList
that takes aIEnumerable<foo>
object like this:Or you could also do something like this:
However as mentioned in some of the other answers, if you are not adding any functionality then you should just create an alias with the
using
keyword.不要声明新类
FooList
。只需使用别名即可。在源文件的顶部写入:现在您可以在任何地方编写
FooList
,它将被视为与List
完全相同。Don’t declare a new class
FooList
. Just use an alias. At the top of your source file, write:Now you can write
FooList
everywhere and it will be treated to be exactly identical toList<Foo>
.