I have a serializable class which contains some properties, including a variable of type object. I want my class to be able to contain different (of course serializable) objects. The problem is, that I get an exception during serialization:
Use the XmlInclude or SoapInclude attribute to specify types that are not known statically
I created a small sample for this issue:
class Program
{
static void Main(string[] args)
{
SerializeableTestClass testClass = new SerializeableTestClass();
testClass.Params = new ExtendedParams();
MemoryStream ms = new MemoryStream();
XmlSerializer xmlf = new XmlSerializer(testClass.GetType());
xmlf.Serialize(ms, testClass);
ms.Capacity = (int)ms.Length;
ms.Close();
byte[] array = ms.GetBuffer();
}
}
[Serializable]
public class SerializeableTestClass
{
[XmlElement(ElementName = "Params")]
public object Params;
}
[Serializable]
public class ParamsBase
{
[XmlElement(ElementName = "SomeValue")]
public Int32 SomeValue;
}
[Serializable]
public class ExtendedParams : ParamsBase
{
[XmlElement(ElementName = "SomeNewValue")]
public Int32 SomeNewValue;
}
Is there a possibility to serialize and deserialize this class without specifying the concrete type of "Params" ???
Best regards