Serialization reflection ExecutionEngineException on iPhone

I’m attempting to save my game with serialization like so:

worldData data = m_WorldData;
Stream stream = File.Open(GetiPhoneDocumentsPath()+"/MySavedGame.game", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Binder = new VersionDeserializationBinder(); 
Debug.Log ("Writing Information");
bformatter.Serialize(stream, data);
stream.Close();

This works fine on the PC, but on iPhone it gives the following reflection error. Does anyone have any idea what the problem may be?

ExecutionEngineException: Attempting to JIT compile method 'List`1__TypeMetadata:.ctor ()' while running with --aot-only.

at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
  at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0 
  at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00000] in <filename unknown>:0 
  at System.Activator.CreateInstance (System.Type type) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.CreateMemberTypeMetadata (System.Type type) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.GetObjectData (System.Object obj, System.Runtime.Serialization.Formatters.Binary.TypeMetadata& metadata, System.Object& data) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObject (System.IO.BinaryWriter writer, Int64 id, System.Object obj) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectInstance (System.IO.BinaryWriter writer, System.Object obj, Boolean isValueObject) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteQueuedObjects (System.IO.BinaryWriter writer) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectGraph (System.IO.BinaryWriter writer, System.Object obj, System.Runtime.Remoting.Messaging.Header[] headers) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) [0x00000] in <filename unknown>:0 
  at WorldGameObject.Save () [0x00000] in <filename unknown>:0 
  at HUD.OnGUI () [0x00000] in <filename unknown>:0

Please see [The game crashes with the error message “ExecutionEngineException: Attempting to JIT compile method ‘SometType`1:.ctor ()’ while running with --aot-only.”][1]

I’ve got the same problem and from google searches am finding that List and Queue types might need JIT in order to work correctly.

On the iPhone JIT is not allowed.

I have yet to find the correct way to resolve this.

Some pages suggest using XmlSerialization instead. However [Stackoverflow What are the differences between xml and binary formatter][2] shows that XML doesnt handle graphs of objects that reference each other, nor private fields. So I’m sticking with BinaryFormatter.

My current workaround is to mark the class containing these generic collections with [Serializable] and implementing this interface ISerializable which requires these two methods (as per [Serialization (C# and Visual Basic)][3])

I then unroll the collection types manually:

    public void GetObjectData (SerializationInfo info, StreamingContext context)
    {
            Object[] queueContents = aQueue.ToArray();
            info.AddValue ("aQueue.count", queueContents.Count());
            for (int i=0; i < queueContents.Count(); i++) {
                    info.AddValue ("aQueue[" + i + "]", queueContents *);*

}
}
// The special constructor is used to deserialize values.
public MyClassname (SerializationInfo info, StreamingContext context)
{
int aQueueCount = info.GetInt32 (“aQueue.count”);
for (int i=0; i < aQueueCount; i++) {
ActualClass obj = (ActualClass)info.GetValue (“aQueue[” + i + “]”, typeof(ActualClass));
aQueue.Enqueue(obj);
}
}
BinaryFormatter includes type meta data in the output stream it knows the real subtypes and can create the right types of objects for you without you having to do any more work.
[1]: Unity - Manual: Troubleshooting
[2]: c# - What are the differences between the XmlSerializer and BinaryFormatter - Stack Overflow
[3]: Serialization (C# and Visual Basic) | Microsoft Learn