- Home /
A type of object being compared to another in serialization
I have some classes with Equals and GetHashCode methods overriden. 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. 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)
Answer by Yokimato · Mar 17, 2014 at 06:19 PM
A more safe implementation to test that the input is actually of type LevelData
before casting it to one since there's no limitation based on the arguments. Here's an example:
public override bool Equals (object obj)
{
LevelData levelData = obj as LevelData;
return levelData != null && (id == levelData.id);
}
The variable levelData
will be null if obj
is NOT of type LevelData.
yeah but is that correct? I mean, that doesnt happen before. Why now serialization is messing, checking a type that isnt supposed to be checked?
This is standard, yes. The implementation of the serializer/de-serializer might run equal checks on types to deter$$anonymous$$e which types to create, which is totally understandable.
Right, you should always verify the type in Equals. For example when you compare your object to null you would also get a null ref exception since "null" don't has a field "id" ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613140458im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Creating a save.xml file and a folder to stash it in 1 Answer
IsolatedStorageException: Could not find a part of the path 0 Answers
C# how to setup a Binary Serialization 4 Answers
Utilizing Unity's default serializer 2 Answers