- Home /
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.
Specify the typemetadata constructor statically so that it doesn't need to be JIT compiled
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!
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.
Answer by Dawn0689 · Jul 12, 2012 at 07:31 AM
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...
Your answer
Follow this Question
Related Questions
Serialization issue with multiple SaveGames iOS - Unity Serializer [SOLVED] 1 Answer
Can Monotouch and Unity coexist? 0 Answers
Is PlayerPrefs still slow? 3 Answers
Using system.xml.linq with Unity iOS 0 Answers
Nullreference on device only 1 Answer