Consider the following scenario.
Document β Section β Body β Elements
The document has sections, the section contains the body. The body has some text and a list of items. The question is what this is about. Sometimes the items are the main list of the string, but sometimes the items contain a list of the user data type.
So:
public class Document { public Section[] Sections{get;set;} } public class Section { public SectionType Type{get;set;} public Body {get;set;} } public class Body {
I can't get this to work. Property elements must be defined by the type to compile. I'm stuck here. I can easily fix this by creating a Section class for each of the sections that I use. But the fact is that all other codes are the same, and all operations on the section will be the same. The only difference is the type of list that is used in the body.
What is the best practice for this. I tried generics, abstractions, etc. I can make it work if you create the Items class directly from the calling program, but I cannot make it work if the elements are declared as a property in another class.
I can provide more details if necessary. Thank you guys and girls for your support.
generics polymorphism c #
Mounhim
source share