Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by matthew.kaemmerer · Sep 09, 2011 at 06:16 PM · iosserialization

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image japanitrat · Sep 09, 2011 at 09:14 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges