在简单类上使用NullReferenceException导致Xml序列化失败
本文关键字:导致 Xml 序列化 失败 NullReferenceException 简单 | 更新日期: 2023-09-27 17:49:41
我已经得到了要序列化的简单类:
public class InnerCharacter : Object {
public double xp;
public double hp;
}
我尝试不同的方法,例如:
public static void TestSerialize()
{
InnerCharacter c = new InnerCharacter();
Serialize(c);
}
static public void Serialize(InnerCharacter details)
{
XmlSerializer serializer = new XmlSerializer(typeof(InnerCharacter));
using (TextWriter writer = new StreamWriter(@"Xml.xml")) {
serializer.Serialize(writer, details);
}
}
我也尝试了文件流和其他方法,但它们都导致相同的异常。以下是我如何尝试在Unity中加载代码,在我的一个GameObjects上:
public void Start()
{
TestSerialize();
}
它们都以相同的NullReferenceException失败:
NullReferenceException
System.Reflection.MonoProperty.GetterAdapterFrame[Object,String] (System.Reflection.Getter`2 getter, System.Object obj) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoProperty.cs:234)
System.Reflection.MonoProperty.GetValue (System.Object obj, System.Object[] index) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoProperty.cs:292)
System.Xml.Serialization.XmlTypeMapMember.GetValue (System.Object ob)
System.Xml.Serialization.XmlSerializationWriterInterpreter.GetMemberValue (System.Xml.Serialization.XmlTypeMapMember member, System.Object ob, Boolean isValueList)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteElementMembers (System.Xml.Serialization.ClassMap map, System.Object ob, Boolean isValueList)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteObjectElementElements (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteObjectElement (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob, System.String element, System.String namesp)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteObject (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob, System.String element, System.String namesp, Boolean isNullable, Boolean needType, Boolean writeWrappingElem)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteRoot (System.Object ob)
System.Xml.Serialization.XmlSerializer.Serialize (System.Object o, System.Xml.Serialization.XmlSerializationWriter writer)
System.Xml.Serialization.XmlSerializer.Serialize (System.Xml.XmlWriter writer, System.Object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces)
XmlSerializer
不能处理继承自UnityEngine.Object
的对象。来源:XML序列化错误。