C# 枚举具有2个类继承性的类的列表
我使用以下代码在我的可枚举代码中创建了一个类:C# 枚举具有2个类继承性的类的列表,c#,inheritance,ienumerable,ienumerator,C#,Inheritance,Ienumerable,Ienumerator,我使用以下代码在我的可枚举代码中创建了一个类: public class ItemCollection : IEnumerable<AbstractItem> { private List<AbstractItem> Items; public IEnumerator<AbstractItem> GetEnumerator() { return Items.GetEnumerator(); } Sys
public class ItemCollection : IEnumerable<AbstractItem>
{
private List<AbstractItem> Items;
public IEnumerator<AbstractItem> GetEnumerator()
{
return Items.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public类ItemCollection:IEnumerable
{
私人清单项目;
公共IEnumerator GetEnumerator()
{
返回项。GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
}
它工作正常,但我遇到了一个问题:
AbstractItem有两个继承类,Book和Journal,它们都继承AbstractItem的对象并有自己的一些类(它们都从AbstractItem获得“Name”,但只有Book有“BGenre”,只有Journal有“JTopic”),因为我实现了AbstractItem的iEnumerator,我不能在foreach循环中使用继承类拥有的对象。如何修复它?foreach(集合中的AbstractItem)
foreach (AbstractItem item in collection)
{
// common book and journal code here
if (item is Book)
{
// some code for books
}
else if (item is Journal)
{
// some code for journals
}
}
// another way:
List<Book> books = collection.OfType<Book>().ToList();
List<Journal> journals = collection.OfType<Journal>().ToList();
{
//常见的书籍和期刊代码在这里
如果(项目为书本)
{
//一些书籍代码
}
else if(项目为日记账)
{
//一些期刊代码
}
}
//另一种方式:
列表书籍=collection.OfType().ToList();
列表日记帐=collection.OfType().ToList();
您可以使用泛型:
public class ItemCollection<T> : IEnumerable<T>
{
private List<T> Items;
public IEnumerator<T> GetEnumerator()
{
return Items.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public类ItemCollection:IEnumerable
{
私人清单项目;
公共IEnumerator GetEnumerator()
{
返回项。GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
}
然后,您可以执行以下操作:
var books = new ItemCollection<Book>();
foreach(var book in books)
{
doSomething(book.BookProperty);
}
var books=newitemcollection();
foreach(账簿中的var账簿)
{
doSomething(book.BookProperty);
}
或
var journals=newitemcollection();
foreach(日记账中的var日记账)
{
剂量测定法(期刊、期刊属性);
}
或者在访问摘要时
var items = new ItemCollection<AbstractItem>();
foreach(var item in items)
{
doSomething(item.AbstractItemProperty);
}
var items=newitemcollection();
foreach(项目中的var项目)
{
doSomething(item.AbstractItemProperty);
}
您可以使用Linq过滤出书籍
s、日志
s任何内容:
ItemCollection collection = ...
var books = collection
.OfType<Book>();
var journals = collection
.OfType<Journal>();
我不知道实现抽象类是否做得很好。但是在这种情况下,您可以使用反射来访问类的属性。像这样:
foreach (AsbtractClass Item in ItemCollection)
{
System.Reflection.PropertyInfo BGenre = Item.GetType().GetProperty("BGenre");
System.Reflection.PropertyInfo JTopic = Item.GetType().GetProperty("JTopic");
if (BGenre != null) TxtResultBGenre.Text = BGenre.GetValue(Item).ToString();
if (JTopic != null) TxtResultJTopic.Text = BGenre.GetValue(Item).ToString();
}
或者,您可以分析项目的数据类型并获得已知属性
foreach (AsbtractClass Item in ItemCollection)
{
if (Item is Book)
TxtResultBGenre.Text = System.Reflection.PropertyInfo BGenre = Item.GetType().GetProperty("BGenre").GetValue(Item).ToString();
else if (Item is Journal)
TxtResultJTopic.Text = System.Reflection.PropertyInfo JTopic = Item.GetType().GetProperty("JTopic").GetValue(Item).ToString();
}
您能否提供代码,说明如何在foreach循环中使用继承类的对象?您可以将集合设置为泛型:public class ItemCollection:IEnumerable,其中T:AbstractItemyou不能执行
var books=new ItemCollection()
因为公共类ItemCollection:IEnumerable
不兼容您需要在类ItemCollection
上定义泛型变量是的,我的错,打字错误,修复了它。
foreach (AsbtractClass Item in ItemCollection)
{
System.Reflection.PropertyInfo BGenre = Item.GetType().GetProperty("BGenre");
System.Reflection.PropertyInfo JTopic = Item.GetType().GetProperty("JTopic");
if (BGenre != null) TxtResultBGenre.Text = BGenre.GetValue(Item).ToString();
if (JTopic != null) TxtResultJTopic.Text = BGenre.GetValue(Item).ToString();
}
foreach (AsbtractClass Item in ItemCollection)
{
if (Item is Book)
TxtResultBGenre.Text = System.Reflection.PropertyInfo BGenre = Item.GetType().GetProperty("BGenre").GetValue(Item).ToString();
else if (Item is Journal)
TxtResultJTopic.Text = System.Reflection.PropertyInfo JTopic = Item.GetType().GetProperty("JTopic").GetValue(Item).ToString();
}