[英]polymorphism, generics and anonymous types C#
请考虑以下情形。
文档 - >部分 - >正文 - >项目
文档有部分,一个部分包含一个正文。 正文包含一些文本和项目列表。 这些项目就是问题所在。 有时这些项是字符串的基本列表,但有时这些项包含自定义数据类型的列表。
所以:
public class Document
{
public Section[] Sections{get;set;}
}
public class Section
{
public SectionType Type{get;set;}
public Body {get;set;}
}
public class Body
{
//I want the items to be depending on the section type.
//If e.g. the sectiontype is experience, I want the Items to be created with type //Experience. If sectiontype is default I want the Items to be created with type string
public Items<T> Items {get;set;}
}
public class Items<T>:IEnumerable, IEnumerator
{
// Do all the plumbing for creating an enumerable collection
}
public class Experience
{
public string Prop1{get;set;}
public string Prop2 {get;set;}
}
我无法让这个工作。 属性Items必须由类型定义才能进行编译。 我被困在这里。 我可以通过为我使用的每种部分创建一个Section类来轻松解决这个问题。 但问题是所有其他代码都是相同的,并且该部分的所有操作都是相同的。 唯一不同的是Body中使用的列表类型。
这是什么最好的做法。 我已经尝试过泛型,抽象等。如果直接从调用程序创建Items类,我可以使它工作,但是如果Items被声明为另一个类的属性,我无法使它工作。
如果需要,我可以提供更多细节。 谢谢你们的支持。
这个课程无效:
public class Body
{
public Items<T> Items {get;set;}
}
您需要在此处定义具体类型,或者也使Body
成为泛型类型。 所以要么:
public class Body<T>
{
public Items<T> Items {get;set;}
}
要么:
public class Body
{
public Items<MyClass> Items {get;set;}
}
为Items创建一个接口
public interface IItems: IEnumerable, IEnumerator{
}
public class Items<T>: IItems
{
// Do all the plumbing for creating an enumerable collection
}
然后在其他地方使用它。
public class Body
{
//I want the items to be depending on the section type.
//If e.g. the sectiontype is experience, I want the Items to be created with type //Experience. If sectiontype is default I want the Items to be created with type string
public IItems Items {get;set;}
}
最明显的选择是声明你的类型对象列表,但是你必须处理拳击和取消装箱对象的性能损失。 我想我会创建一个界面来定义你要从项目中寻找的行为,并确保每个项目实现该界面。
public IMyInterface
{
string dosomething();
}
public Items<IMyInterface> Items {get;set;}
然后,当您迭代它们时,您可以要求每个项目执行一些有用的操作。
这可能对你有用:
public class Document<T> where T: IEnumerable, IEnumerator
{
private Section<T>[] Sections{get;set;}
}
private class Section<T>
{
private Body<T> body {get;set;}
}
private class Body<T>
{
private Items<T> Items {get;set;}
}
private class Items<T>:IEnumerable, IEnumerator
{
// Do all the plumbing for creating an enumerable collection
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
/* Needed since Implementing IEnumerator*/
public bool MoveNext()
{
return false;
}
public void Reset()
{
}
public object Current
{
get{ return new object();}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.