SerializationException:在c#unity3d中找不到类型' System.Collections.Generic.List`1

时间:2017-09-09 05:14:45

标签: c# .net generics unity3d serialization

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

  

错误: SerializationException:找不到类型' System.Collections.Generic.List`1 [[ABC,Assembly-CSharp,Version = 1.0.2.18931,Culture = neutral,PublicKeyToken = NULL]]&#39 ;.

当我将对象保存到文件并将其从文件中加载时,我没有发生这种情况,而我正在玩游戏但没有停止游戏。

但是如果我停止游戏并更改任何代码行(与序列化和反序列化无关)并从之前保存的文件加载数据并尝试反序列化我得到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中的日志。所以它意味着BindToType中的函数SerilizationBinder

2 个答案:

答案 0 :(得分:9)

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

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

通常情况下,我会将此作为重复投票,但我不能因为公开的赏金,所以我决定回答;)

答案 1 :(得分:0)

对于快速二进制序列化Protocol Buffers可能是个不错的选择。此外,它应解决兼容性问题(您可以手动控制字段顺序/排除)