- Home /
The question is answered, right answer was accepted
Using BinaryFormatter for deserialization - ArgumentException: method argument length mismatch
So i'm trying to serialize and deserialize a few basic classes, I get this very cryptic error:
ArgumentException: method argument length mismatch
System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, Boolean throwOnBindFailure)
And I really have absolutely no idea why. My code contains no delegates, no reflection, nothing but basic data structures.
Any tips?
Here's the error in full.
ArgumentException: method argument length mismatch
System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Delegate.cs:226)
System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Delegate.cs:276)
System.Runtime.Serialization.SerializationCallbacks.Invoke (System.Collections.ArrayList list, System.Object target, StreamingContext context) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization/SerializationCallbacks.cs:110)
System.Runtime.Serialization.SerializationCallbacks.RaiseOnDeserialized (System.Object target, StreamingContext contex) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization/SerializationCallbacks.cs:136)
System.Runtime.Serialization.ObjectManager.RaiseOnDeserializedEvent (System.Object obj) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization/ObjectManager.cs:186)
System.Runtime.Serialization.ObjectManager.RaiseDeserializationEvent () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization/ObjectManager.cs:162)
System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject (System.IO.BinaryReader reader) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:147)
System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectGraph (BinaryElement elem, System.IO.BinaryReader reader, Boolean readHeaders, System.Object& result, System.Runtime.Remoting.Messaging.Header[]& headers) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:110)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:179)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:136)
DataLoader.DeserializeObject (System.String str) (at Assets/Code/Title/DataLoader.cs:237)
DataLoader+<LoadGameBinary>c__Iterator0.MoveNext () (at Assets/Code/Title/DataLoader.cs:43)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
As you can see from the stacktrace the error is thrown inside the "Deserialize" method of the BinaryFormatter. So you most likely have something setup the wrong way. How do your classes look like?
You might have a class that you deserialize that has a method with one of these attributes:
OnSerializingAttribute
OnSerializedAttribute
OnDeserializingAttribute
OnDeserializedAttribute
However that method most likely has the wrong signature. It should have just a Strea$$anonymous$$gContext parameter. The BinaryFormatter will construct a multicast delegate out of all methods that have such an attribute. Since the error comes from "RaiseOnDeserialized" you most likely have the attribute "OnDeserialized" on a wrong method.
Follow this Question
Related Questions
EndOfStreamException : Failed to read past end of stream 1 Answer
EndOfStreamException : Failed to read past end of stream 1 Answer
How to add strings in a list AND be able to save it and load it? Using playerpref OR serialization 2 Answers
Why has my BinaryFormatter stopped working? 2 Answers
how to deserialize json with arbitrary string keys using JsonUtility(unity c#)? 4 Answers