如何将同一基类的子类放入列表中?
如何将同一基类的子类放入列表中?
我正在使用 ASP.NET MVC3,并创建了一个包含姓名、年龄等属性的基本模型类。现在我已经创建了包含更多细节的子模型(子类)。 为了轻松处理子类,我想要一个包含其中对象的列表,但如何处理?
我读过有关接口或 ICollection 等的内容,但不知道什么是正确的选择以及如何开始:-(
How can I put subclasses from the same baseclass into a list?
I´m working with ASP.NET MVC3 and have created a basemodel-class with properties like name, age and so on. Now i have created submodels (subclasses) with more details.
To easily handle the subclasses i want a list with the objects in it but how?
I have read about interfaces or ICollection and so on but don´t know what´s the right choice and how to start :-(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需创建列表,使其属于基类:
然后像平常一样添加子类对象:
等等。其中:
然后当您将它们取出时,您可以使用
is
和as
运算符来确定它们实际上是什么类型并适当地处理它们。Just create the list so it's of the base class:
then add your subclass objects as normal:
etc. where:
Then when you get them out you can use the
is
andas
operators to determine what type they actually are and deal with them as appropriate.我
推荐
I'd recommend
for