我在序列化对象实例时遇到了一点问题,让一些伪代码自己说话:
List<A> ListOfA = new List<A>();
[Serializable]
public class A
{
public Object Instance;
...
}
[Serializable]
public class B
{
public String SomeAttribute = "example";
}
// This will be called:
void Serialize()
{
var a = new A();
a.Instance = new B();
ListOfA.Add(a);
// a.Instance = new String("test"); works but List<String>, B, ... throws Invalid Operation Exception when serializing:
using (TextWriter textWriter = new StreamWriter(filePath, false))
{
var xmlSerializer = new XmlSerializer(ListOfA.GetType());
xmlSerializer.Serialize(textWriter, ListOfA );
}
}
有人对此有解决方案吗?
我尝试使用XmlInclude
没有任何结果。
甚至可以在不知道其类型的情况下序列化 Object 吗?