c# - 序列化异常 : Could not find type 'System.Collections.Generic.List` 1 in c# unity3d

标签 c# .net generics unity3d serialization

我正在尝试在 c# unity3d 中序列化和反序列化一个对象。为此,我使用下面的代码。但是我收到下面提到的错误。

Error: SerializationException: Could not find type 'System.Collections.Generic.List`1[[ABC, Assembly-CSharp, Version=1.0.2.18931, Culture=neutral, PublicKeyToken=null]]'.

当我在不停止游戏的情况下玩游戏时将对象序列化保存到文件并从文件加载它时,这不会发生。

但是,如果我停止游戏并更改任何代码行(与序列化和反序列化无关)并从之前保存的文件中加载数据并尝试反序列化,则会发生错误,我得到一个 SerializationException

我用的是visual studio编辑器,unity3d版本是5.5.4

我可能遗漏了一些非常简单的东西。有人可以帮我解决这个问题。

谢谢。

public static string SerializeObject<T>(T objectToSerialize)
{
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream memStr = new MemoryStream();

    try
    {
        bf.Serialize(memStr, objectToSerialize);
        memStr.Position = 0;

        return Convert.ToBase64String(memStr.ToArray());
    }
    finally
    {
        memStr.Close();
    }
}

public static T DeserializeObject<T>(string str)
{
    BinaryFormatter bf = new BinaryFormatter();
    bf.Binder = new CurrentAssemblyDeserializationBinder();
    byte[] b = Convert.FromBase64String(str);
    MemoryStream ms = new MemoryStream(b);

    try
    {
        return (T)bf.Deserialize(ms);
    }
    finally
    {
        ms.Close();
    }
}

我正在使用的类:

    [Serializable]

  public class ABC : ISerializable
  {
    public SerializableDictionary<int, ExampleClass> a = new SerializableDictionary<int, ExampleClass>();

    public GameObject b;
    public GameObject c;
    public bool d = false;
    public ABC e;
    public int f;
    public string g = "";
    public int h = -1;
    public int i = -1;
    public int j = -1;
    public string k = "default";
    public XYZ l = XYZ.P;
  }

    [Serializable]
    public enum XYZ
    {
        P,
        Q
    }


    [Serializable()]
    public class ABCListWrapper : ISerializable
    {

    public List<ABC> abcMappings = new List<ABC>();
    public string version = "1.53";
    public float interval;
    }


    //Serilization 
    abcLW = new ABCListWrapper();
    abcW = getABCListWObj();
    string abcWString = SerializeObject(abcW);
    File.WriteAllText(Application.streamingAssetsPath + "/filename.json", abcWString);


    //Deserilization call 
    ABCListWrapper l = new ABCListWrapper();
    string l_1 = File.ReadAllText(Application.streamingAssetsPath + "/filename.json");
    l =  DeserializeObject<ABCListWrapper>(l_1);

尝试解决问题:

public sealed class CurrentAssemblyDeserializationBinder : SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        Version assemVer1 = Assembly.GetExecutingAssembly().GetName().Version;
        Debug.Log("ASSEM VER: " + assemVer1 + "--NAME--" + assemblyName + " --OVERAL-- " + Assembly.GetExecutingAssembly().FullName + " --TYPE-- " + typeName );
        //assemblyName = Assembly.GetExecutingAssembly().FullName.Replace(assemVer1.ToString(), "1.0.2.23455");
        //string assemblyNameCustom = "Assembly-CSharp, Version=1.0.2.18931, Culture=neutral, PublicKeyToken=null";

        bool isList = false;

        if (typeName.Contains("List`1"))
            isList = true;
        // other generics need to go here

        if (isList)
            return Type.GetType(string.Format("System.Collections.Generic.List`1[[{0}, {1}]]", "ABC", "Assembly-CSharp, Version=1.0.2.18931, Culture=neutral, PublicKeyToken=null"));
        else
            return Type.GetType(string.Format("{0}, {1}", assemblyName, typeName));

    }

}

但我遇到了同样的异常,BindToType 中的日志从未打印出来。所以它意味着 SerilizationBinder 中的函数 BindToType

最佳答案

二进制格式化程序将程序集版本信息与序列化类型一起存储。因此,即使类型未更改,序列化/反序列化也会在程序集更新后立即更改。

有关详细信息,请参阅此相关问答:Binary Deserialization with different assembly versionthe relevant MSDN article .

通常我会将其作为重复项投票,但我不能因为公开赏金,所以我决定改为回答 ;)

关于c# - 序列化异常 : Could not find type 'System.Collections.Generic.List` 1 in c# unity3d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46127352/

相关文章:

c# - 如何通过动态编译使用自定义类

.net - SagePay 直接集成套件 v4.00

.net - 替代dompdf(将HTML + CSS转换为PDF的PHP工具)但是在.NET中?

.net - 使用 .net 库自动执行任务的最佳技术是什么?

c# - 构建通用过滤器参数输入接口(interface)?

Java泛型问题

c# - 如何在操作系统检测到 Ctrl-Alt-Del 之前或之后检测到它?

c# - 网络客户端错误(随机且非常烦人)

c# - 有没有办法在 C# 中限制 API 的处理器资源?

c# - 无法将 List<int[*]> 转换为用反射实例化的 List<int[]>