【发布时间】:2018-11-05 17:07:10
【问题描述】:
我用代码序列化了一个类:
public void Save()
{
string fichero = Application.persistentDataPath + "/" + nombreJuego + ".dat";
FileStream file = File.Create(fichero);
DataContractSerializer bf = new DataContractSerializer(typeof(JuegoMesa));
MemoryStream streamer = new MemoryStream();
bf.WriteObject(streamer, this);
streamer.Seek(0, SeekOrigin.Begin);
file.Write(streamer.GetBuffer(), 0, streamer.GetBuffer().Length);
file.Close();
}
并反序列化它:
public void Load()
{
string fichero = Application.persistentDataPath + "/" + nombreJuego + ".dat";
Debug.Log(fichero);
DataContractSerializer bf = new DataContractSerializer(typeof(JuegoMesa));
try
{
JuegoMesa leido = null;
object objeto;
MemoryStream streamer = new MemoryStream(File.ReadAllBytes(fichero));
streamer.Seek(0, SeekOrigin.Begin);
objeto = bf.ReadObject(streamer);
leido = (JuegoMesa)objeto;
ActualizarListas(leido.listaListas);
ActualizarPropiedades(leido.listaPropiedades);
ActualizarRecursos(leido.listaRecursos);
ActualizarComponentes(leido.listaComponentes);
}
catch (FileNotFoundException)
{
listaListas.Clear();
listaPropiedades.Clear();
listaRecursos.Limpiar();
listaComponentes.Clear();
Save();
}
}
在读取对象时给了我一个例外:
XmlException:找不到类型;名称:PropiedadTexto,命名空间:http://schemas.datacontract.org/2004/07/ System.Runtime.Serialization.XmlFormatterDeserializer.GetTypeFromNamePair (System.String name, System.String ns)
该类具有以下要序列化的元素:
public string nombreJuego;
public List<TextosPredefinidos> listaListas;
public List<Propiedad> listaPropiedades;
public ListaRecursos listaRecursos;
public List<ListaRecursos> listaComponentes;
List<Propiedad>
是从 Propiedad 类派生的类的对象列表。以错误的类为例
[Serializable]
public class PropiedadTexto : Propiedad
{
public string textoDescriptivo;
public PropiedadTexto() : base()
{
}
...
}
有谁知道可能是什么问题?
为我糟糕的英语道歉。
谢谢。
【问题讨论】:
-
我怀疑使用序列化器和使用反序列化器时的代码版本不同。有人修改了一个类并添加/删除了一个属性。
-
您能否请edit 分享一个重现问题的minimal reproducible example?如果我们可以自己测试和调试问题,我们更有可能能够提供帮助;请参阅How to Ask 了解更多信息。
标签: c# xml serialization enums datacontractserializer