x


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!

more ▼

asked Sep 09 '11 at 06:16 PM

matthew.kaemmerer gravatar image

matthew.kaemmerer
1 1 1 1

which line is the error? the cast maybe? indeed very weird, because i already serialized enums, although with the xmlserializer. Did you test for a single enum (not array), too?

also, why do you need this custom serialization. i think you can just use the Serializable attribute on all types you want to (de)serialize and let the serializer do all the work for you.

Sep 09 '11 at 09:14 PM japanitrat
(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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

more ▼

answered Jul 12 '12 at 07:31 AM

Dawn0689 gravatar image

Dawn0689
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1953
x176
x6

asked: Sep 09 '11 at 06:16 PM

Seen: 1052 times

Last Updated: Jul 12 '12 at 07:31 AM