I have a generic class that represents a document that can be of currently only two types. Type1 or Type2. Most of the methods and properties work perfectly for both types but the title is different. What I am wondering is if there is a better way to handle this? Thanks!!
[XmlIgnore]
public string DocumentType
{
get
{
return typeof(T).Name;
}
}
[XmlIgnore]
public string DocumentTitle
{
get
{
string retval = string.Empty;
Object obj = Document;
switch (DocumentType)
{
case "Type1":
retval = ((Type1)obj).title.Text;
break;
case "Type2":
retval = ((Type2)obj).Title;
break;
}
return retval;
}
}
Type1 and Type2 were generated using xsd.exe so I hesitate to change them although maybe adding a readonly xml ignored property to get the title in both Type1 and Type2 that is consistent?
Use a common interface and implement it in each class. If you don’t want to change the original class you could try adding a wrapper around each class that implements this interface.
Then you can do this:
You might want to extend the interface to
IDocumentand include all the other common members such asName, etc.