C#泛型列表的二进制序列化和反序列化

C#泛型列表的二进制序列化和反序列化,c#,unity3d,serialization,deserialization,C#,Unity3d,Serialization,Deserialization,我在反序列化过程中遇到一个错误: SerializationException: Could not find type 'System.Collections.Generic.List`1[[ExampleNamespace.ExampleClass, Assembly-CSharp, Version=0.1.6279.22118, Culture=neutral, PublicKeyToken=null]]'. BinaryFormatter formatter = new BinaryF

我在反序列化过程中遇到一个错误:

SerializationException: Could not find type 'System.Collections.Generic.List`1[[ExampleNamespace.ExampleClass, Assembly-CSharp, Version=0.1.6279.22118, Culture=neutral, PublicKeyToken=null]]'.
BinaryFormatter formatter = new BinaryFormatter();
FileStream file = File.Open(saveFilePath, FileMode.Open);

SaveStructure saveStructure = (SaveStructure)formatter.Deserialize(file);
file.Close();
首先,我序列化了一个自定义的[Serializable]类(saveStructure):

它包含几个字段以及一个
列表
字段。 MyCustomClass也是一个[Serializable]

它似乎可以正确序列化结构,但在反序列化过程中无法读回:

SerializationException: Could not find type 'System.Collections.Generic.List`1[[ExampleNamespace.ExampleClass, Assembly-CSharp, Version=0.1.6279.22118, Culture=neutral, PublicKeyToken=null]]'.
BinaryFormatter formatter = new BinaryFormatter();
FileStream file = File.Open(saveFilePath, FileMode.Open);

SaveStructure saveStructure = (SaveStructure)formatter.Deserialize(file);
file.Close();
我知道XmlSerializer,但是我不想向用户公开数据

提前谢谢

编辑:


我已经通过使用数组而不是列表解决了我的问题,但是,如果有人为列表问题提供解决方案,我仍然会非常感激

“我知道XmlSerializer,但我不想向用户公开数据。”我一直听到这是不应该使用Json、Xml或PlayerPrefs的原因。二进制格式化程序可以反转。就是这样。您可以使用xml或xml,然后在保存之前对字符串进行加密。您可能会发现这篇文章很有帮助。要回答您的问题,Unity中的BinaryFormatter存在问题。我给您的第一个建议是确保中的
SaveStructure
类在没有任何其他类的情况下在另一个文件中声明。必须放入的文件应如下所示:
SaveStructure.cs
。不要在那里放置任何其他文件,也不要在其他现有类中声明该类。如果失败,则使用或xml。“我知道XmlSerializer,但我不想向用户公开数据。”我一直听到这是不应使用Json、xml或PlayerPrefs的原因。二进制格式化程序可以反转。就是这样。您可以使用xml或xml,然后在保存之前对字符串进行加密。您可能会发现这篇文章很有帮助。要回答您的问题,Unity中的BinaryFormatter存在问题。我给您的第一个建议是确保中的
SaveStructure
类在没有任何其他类的情况下在另一个文件中声明。必须放入的文件应如下所示:
SaveStructure.cs
。不要在那里放置任何其他文件,也不要在其他现有类中声明该类。如果失败,则使用xml或xml。