简体   繁体   English

在序列化中将一种对象与另一种对象进行比较

[英]A type of object being compared to another in serialization

I have some classes with Equals and GetHashCode methods overriden. 我有一些使用Equals和GetHashCode方法覆盖的类。 Like this: 像这样:

    // LevelData.cs
    public override bool Equals (object obj)
    {
        return id == ((LevelData)obj).id;
    }

When I try to save an object (via serialization), a type of object is compared to another inside a Equals. 当我尝试保存对象(通过序列化)时,会将一种对象与Equals中的另一种对象进行比较。 I dont understand how. 我不明白怎么做。 This is the erro i get: 这是我得到的错误:

InvalidCastException: Cannot cast from source type to destination type.
LevelData.Equals (System.Object obj) (at Assets/Scripts/Map/LevelData.cs:50)
System.Collections.Hashtable.KeyEquals (System.Object item, System.Object key) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Collections/Hashtable.cs:679)
System.Collections.Hashtable.Find (System.Object key) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Collections/Hashtable.cs:730)
System.Collections.Hashtable.Contains (System.Object key) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Collections/Hashtable.cs:494)
System.Runtime.Serialization.SerializationObjectManager.RegisterObject (System.Object obj) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Runtime.Serialization/SerializationObjectManager.cs:50)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.GetObjectData (System.Object obj, System.Runtime.Serialization.Formatters.Binary.TypeMetadata& metadata, System.Object& data) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:389)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObject (System.IO.BinaryWriter writer, Int64 id, System.Object obj) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:306)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectInstance (System.IO.BinaryWriter writer, System.Object obj, Boolean isValueObject) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:293)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteQueuedObjects (System.IO.BinaryWriter writer) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:271)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectGraph (System.IO.BinaryWriter writer, System.Object obj, System.Runtime.Remoting.Messaging.Header[] headers) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:256)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:232)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:211)
PlayerProfile.Save () (at Assets/Scripts/PlayerProfile.cs:95)

As a guess, objects of type LevelData are compared to the objects of another types during serialization. 作为猜测,在序列化期间,将LevelData类型的对象与其他类型的对象进行比较。 Try to check the Type of the obj before casting: 尝试在转换之前检查objType

public override bool Equals (object obj)
{
    return obj is LevelData ? id == ((LevelData)obj).id : false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM