问题描述
我在用C#编写并为iPhone构建的Unity3D项目中发现了一个奇怪的行为.
有时 IList对象(实现IList接口的类型的对象)丢失有关元素类型的信息.在我的研究过程中,我发现发生在向IList进行类型转换的地方(例如,从 ArrayList 到 IList 的地方)并且 ToString()方法是叫.
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.
这是我从脚本的 Start()方法调用的一种简单测试方法.
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);
}
这是输出:
---
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
如您在调用 list.ToString()后所看到的,有关元素的信息丢失.
As you can see after list.ToString() was called information about element was lost.
请注意,并非每次都会复制此问题.从Xcode启动4次或更多次(不从Unity重新构建)之后,我在iPhone 4s(iOS 5.01)上重现了它.
我正在使用Unity 3.5.0b6,许可证类型:Unity,iPhone,Android.
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'm using Unity 3.5.0b6, license type: Unity, iPhone, Android.
有什么想法可能会发生这种情况吗?
Is there any idea why this may happen?
提前谢谢!
PS *我打开了另一个与此问题相关的主题:
PS* I have opened another thread about issue that can be related to this: C# List<object> to IList cast bug in Unity3d
推荐答案
基于我所看到的.这正是应该发生的行为.
Based on what I can see. This is exactly the behavior that should happen.
这会将集合IList
转换为string
,因此 liststring 的字符串值将是等于 System.Collections.IList 的string
值. >如果我没记错的话.
This converts the collection which is an IList
to a string
so the string value of liststring would be string
value equal to System.Collections.IList if I am not mistaken.
您基本上是在那一行上执行Type.ToString()
.至于ArrayList中的类型似乎正在改变的原因,老实说我无法解释,我怀疑发生了一些不正常的事情.
You are basically doing Type.ToString()
on that line. As to the reason it appears the type in the ArrayList is changing, I honestly cannot explain that, I suspect something non-sinister is going on.
很容易成为
这至少会将liststring
的值设置为ArrayList
列表
That would at least set the value of liststring
to the first element of the ArrayList
list
这篇关于在iOS上的Unity3d中使用IList时C#代码的无效行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!