It is often useful to have abstract classes with several derived types to allow the use of strongly typed lists and such.
For example, you might have a DocumentFragment class that is abstract and two specific classes called TextDocumentFragment and CommentDocumentFragment (this example from Willis).
This allows you to create a List property that can contain objects of only these two types.
If you try to create a WebService that returns this list, you will get an error, but it is easy to get around with the code below ....
[Serializable()] [System.Xml.Serialization.XmlInclude(typeof(TextDocumentFragment))] [System.Xml.Serialization.XmlInclude(typeof(CommentDocumentFragment))] public abstract class DocumentFragment { ...}
The XmlInclude attributes indicate to the class that it can be serialized for these two derived classes.
This generates an attribute in the DocumentFragment element that defines the actual type, as shown below.
<DocumentFragment xsi:type="TextDocumentFragment">
Any additional properties specific to the derived class will also be included using this method.
source share