1
系列化派生類我有這樣的代碼:問題在C#
[Serializable]
public class A {
public int X { get; set; }
}
[Serializable]
public class B : A{
}
[Serializable]
public class C {
public A A { get; set; }
}
...
public string Serialize<T>(T obj)
{
StringBuilder stringBuilder = new StringBuilder();
TextWriter stringWriter = new StringWriter(stringBuilder);
XmlWriter xmlWriter = new XmlTextWriter(stringWriter);
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(xmlWriter, obj);
xmlWriter.Close();
stringWriter.Close();
return stringBuilder.ToString();
}
private void Run() {
C c = new C() {A = new B()};
string str = Serialize(c);
}
我在文字"There was an error generating the XML document."
的string str = SerializationManager.Serialize(c);
執行我該怎麼寫我的代碼序列化c
無一例外System.InvalidOperationException
?