Sof*_*ion 1 java xamarin.android
在monodroid中,我有一个java绑定,它返回一个Java.Lang.Enum对象.
当我尝试将此对象转换为int时,它会抛出.
System.InvalidCastException: Cannot cast from source type to destination type.
Run Code Online (Sandbox Code Playgroud)
这是我在调试器的即时窗口中得到的:
state
{OPENING}
base: {Java.Lang.Enum}
IsClosed: false
IsOpened: false
ThresholdClass: 0x1d200832
ThresholdType: {System.MonoType}
Run Code Online (Sandbox Code Playgroud)
我很惊讶枚举不能转换为int?
您可以使用java.lang.Enum.ordinal()返回一个int值,该值表示Enum类在该类型对象的Enum类定义中的位置.
例如,如果Enum类的定义是这样的:
enum Example {
Cat, Dog, Fish, Goat
}
Run Code Online (Sandbox Code Playgroud)
然后Cat.ordinal()返回int值0,Dog.ordinal()返回int值1,Fish.ordinal()返回int值2,依此类推.
但是,Enum常量的序数位置不能保证保持不变,因此在不确定Enum定义的更改(将改变序数值)不会破坏您的代码时,永远不应该使用它.
| 归档时间: |
|
| 查看次数: |
771 次 |
| 最近记录: |