I\'m trying to figure out how to serialize any class using the XmlSerializer, without using the XmlInclude attribute. Generally works, but with the followi
The serialization side is easy - instead of using typeof(ReactObject) when getting the serializer, use list.GetType(), which will return AList rather than ReactObject and create the correct serializer. If you are worried about instantiating too many serializers you can always keep them in a dictionary keyed by type.
Deserialization is harder as you have to know the type of object first - if you can't get the caller to tell you what type to expect, you will need to read the data before you try deserializing to determine the type.
Does ReactListObject
inherit from ReactObject
? ser1
is instantiated to serialize types of ReactObject
, but from your code sample I can only tell that list
is of type ReactListObject
.
First of all should not it be like this:
//[XmlInclude(typeof(B))]
[Serializable]
public class A : ReactListObject
{
public int AValue = 1;
}
[XmlInclude(typeof(A))]
public class B : A
{
public int BValue = 2;
}