Binary serialization of Enums on iOS

So I’m trying to serialize some data including enums on iOS, but I keep hitting a wall. Here’s the offending code:

public class GameData : ISerializable
{
    public enum Rank
    {
       OneStar,
       TwoStar,
       ThreeStar,
       FourStar
    };
    public Rank[] ranks;

    public GameData(SerializationInfo info, StreamingContext ctx)
    {
        ranks = (Rank[]) info.GetValue("ranks", typeof(Rank[]));
    }
    public void GetObjectData(SerializationInfo info, StreamingContext ctx)
    {
        info.AddValue("ranks", ranks, typeof(Rank[]));
    }
}

Seems like it should be pretty cut-and-dry, no? Not so fast! Mono relies on JIT compilation to serialize Enums, and iOS WILL NOT JIT! I get this error while trying to serialize an instance of GameData:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ExecutionEngineException: Attempting to JIT compile method 'Rank__TypeMetadata:.ctor ()' while running with --aot-only.

Okay, to me this suggests two possible workarounds.

  1. Specify the typemetadata constructor statically so that it doesn’t need to be JIT compiled
  2. Don’t serialize enums.

Problem with option 1 is that using TypeMetadata seems to be rather poorly documented. It is certainly possible, but I haven’t the slightest idea how to do it.

Problem with option 2 is… well it just plain doesn’t make a difference for some reason. Have a look at the modified code:

    public GameData(SerializationInfo info, StreamingContext ctx)
    {
        int[] int_ranks = (int[]) info.GetValue("ranks", typeof(int[]));
        ranks = (Rank[]) int_ranks.Cast<Rank>();
    }
    public void GetObjectData(SerializationInfo info, StreamingContext ctx)
    {
        int[] int_ranks = (int[]) ranks.Cast<int>();
        info.AddValue("ranks", int_ranks, typeof(int[]));
    }

…and here’s the accompanying error:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ExecutionEngineException: Attempting to JIT compile method 'Rank__TypeMetadata:.ctor ()' while running with --aot-only.

In case you’re wondering, yes, this is the same error as before. Why does MonoTouch think I’m still trying to serialize Ranks?! I’m quite clearly serializing ints!

I am also having the same problem, although i didn’t try to serialize enums in my code :frowning:
Does anybody have any clue with this problem ? I seems to keep hitting on walls…