问题
I have class A
inherits from class B
. I have a list of class B
that contains A
and B
items:
List<B> myList = new List<B>();
myList.Add(new A());
When I try to serialize this list using XmlSerializable
, an exception is thrown. If I define the list to of type B, then I do not get this exception. What is the best way to serialize the derived class?
回答1:
XmlIncludeAttribute will do the trick.
If you go to this url, you will find a code sample there too.
Also, in the example the attribute is applied to the method, but you might want to apply it class A in your case.
来源:https://stackoverflow.com/questions/6172330/xml-serializing-a-list-of-a-base-class