Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 vladogol · Mar 29, 2012 at 06:28 PM · c#iphonelisttypecasting

Invalid behavior of C# code while using IList on iOS

I have found a strange behavior in my Unity3D project written in C# and built for iPhone.
Sometimes IList objects (objects of types which implement IList interface) lose information about elements types. During my research I figured out that it occurs when typecast to IList had a place (f.e. from ArrayList to IList) and ToString() method was called.

Here is a simple test method I called from Start() method of my script.

 public void IListToStringTest() {
   ConsolePrintln("---\nIListToStringTest");
   IList list = new ArrayList(); // <-- ARRAYLIST IS TYPECASTED TO ILIST
  
   list.Add(1);
  
   ConsolePrintln("Before \"listString = list.ToString()\"");
  
   ConsolePrintln("list[0] = " + list[0].ToString());
   ConsolePrintln("list[0].GetType().ToString() = " + list[0].GetType().ToString());
   ConsolePrintln("list[0] = " + list[0].ToString());
   ConsolePrintln("list[0].GetType().ToString() = " + list[0].GetType().ToString());
   ConsolePrintln("list[0] = " + list[0].ToString());
   ConsolePrintln("list[0].GetType().ToString() = " + list[0].GetType().ToString());
  
   string listString = list.ToString();
  
   ConsolePrintln("After \"listString = list.ToString()\"");
  
   ConsolePrintln("list[0] = " + list[0].ToString()); // <-- TYPE INFO IS ALREADY LOST
   ConsolePrintln("list[0].GetType().ToString() = " + list[0].GetType().ToString());
   ConsolePrintln("list[0] = " + list[0].ToString());
   ConsolePrintln("list[0].GetType().ToString() = " + list[0].GetType().ToString());
   ConsolePrintln("list[0] = " + list[0].ToString());
   ConsolePrintln("list[0].GetType().ToString() = " + list[0].GetType().ToString());
  
   ConsolePrintln("listString = " + listString);
  
   int intFromList = (int)list[0]; // <-- InvalidCastException IS THROWN HERE
  
   ConsolePrintln("intFromList = " + intFromList);
 }

And here is an output:

 ---
 IListToStringTest
 Before "listString = list.ToString()"
 list[0] = 1
 list[0].GetType().ToString() = System.Int32
 list[0] = 1
 list[0].GetType().ToString() = System.Int32
 list[0] = 1
 list[0].GetType().ToString() = System.Int32
 After "listString = list.ToString()"
 list[0] = System.Collections.ArrayList
 list[0].GetType().ToString() = System.String
 list[0] = System.Collections.ArrayList
 list[0].GetType().ToString() = System.String
 list[0] = System.Collections.ArrayList
 list[0].GetType().ToString() = System.String
 listString = System.Collections.ArrayList
 System.InvalidCastException: Cannot cast from source type to destination type.
   at PPSSerializingTest.IListToStringTest () [0x001ba] in /Users/vlad/Projects/Unity/PPSSerializingTest/Assets/PPSSerializingTest.cs:171
   at PPSSerializingTest.Start () [0x00000] in /Users/vlad/Projects/Unity/PPSSerializingTest/Assets/PPSSerializingTest.cs:16

As you can see after list.ToString() was called information about element was lost.

Note, that this issue is not reproduced every time. I reproduced it on iPhone 4s (iOS 5.01) after 4 or more launches from Xcode (without rebuilding it from Unity).
I assume this issue can be related to full-aot compiler mode, because on Android this code works well.
I'm using Unity 3.5.0b6, license type: Unity, iPhone, Android.

Is there any idea why this may happen?

Thanks in advance!

PS* I have opened another thread about issue that can be related to this: http://answers.unity3d.com/questions/233437/c-list-to-ilist-cast-bug.html

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kryptos · Mar 29, 2012 at 09:57 PM

You should not use ArrayList but generic version of List (System.Collections.Generic).

Comment
Add comment · Show 1 · 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
avatar image vladogol · Mar 30, 2012 at 09:49 AM 0
Share

Can you please explain, why do you think so? I don't know exactly the type of elements, because I use ArrayList to store deserialization result, moreover elements can have different types.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Adding instances of objects into generic list 1 Answer

How to repeat the same list of buttons? 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