从对象强制转换为UInt32会引发InvalidCastException [英] Casting from Object To UInt32 throws InvalidCastException

查看:75
本文介绍了从对象强制转换为UInt32会引发InvalidCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Int32 i = 1;
UInt32 u = (UInt32)i; //OK u gets 1

Object o = i;
u = (UInt32)o; //throws System.InvalidCastException


我不明白为什么.


I don''t understand why. Is this a .Net bug?

推荐答案

这是因为强制转换运算符可以做两件事,而您在这里使它们感到困惑.
第一个示例是 converting 强制转换;该值是一个int,而您将其强制转换为uint.这被定义为有效的转换演员表.如果它是用户定义的类型,将有一个形式为
的运算符
It''s because the cast operator can do two things, and you''re confusing them here.

The first example is a converting cast; the value was an int, and you are casting it to a uint. This is defined to be a valid converting cast. If it were a user defined type, there would be an operator of the form
public static explicit operator uint(int value){ ... }


也有隐式转换演员表,例如


There are also implicit converting casts, for example

int i = 2;
double d = i;


在数字类型中,所有转换类型转换(无论如何,我都认为是有效的).那些可能会丢失数据或更改其解释(缩小和带符号的无符号转换)的是显式的,那些不能(扩展的)隐式的.


第二种类型的转换是类型重新分配.仅当您要强制转换为的类型为 的类型(或具有该类型的显式强制转换运算符)时,此方法才有效.没有从对象到uint定义的显式转换强制转换,而o实际上是int而不是uint,因此强制转换失败.

在这种情况下,您需要做的是首先使用类型重新分配类型转换(因为o实际上是一个int)而获得int,然后在该类型上使用转换类型:


Within the numeric types, all converting casts are (I think, anyway) valid; those which could possibly lose data or change its interpretation (narrowing and signed-unsigned conversions) are explicit, those which can''t (widening) are implicit.


The second type of cast is a type reassignment. This only works if the thing you''re trying to cast actually is of the type you''re trying to cast it to (or has an explicit cast operator from that type). There is no explicit conversion cast defined from object to uint, and o is actually an int, not a uint, so the cast fails.

What you have to do in this situation is first get the int using a type reassignment cast (which will work because o actually is an int), and then use a converting cast on that:

uint u = (uint)(int)o;


使用Convert.ToUInt32方法.
http://msdn.microsoft.com/zh-我们/library/system.convert.touint32%28v=VS.100%29.aspx
Use Convert.ToUInt32 Method.
http://msdn.microsoft.com/en-us/library/system.convert.touint32%28v=VS.100%29.aspx
Int32 i = 1;
UInt32 u = (UInt32)i;

Object o = i;
u = Convert.ToUInt32(o);



我看到的一个不支持"u = (UInt32)o;"的原因是-数据类型UInt32不符合CLS.
http://msdn.microsoft.com/en-us/library/system.uint32.aspx

下面的链接也很容易理解.
http://msdn.microsoft.com/en-us/library/system.iconvertible.aspx
http://msdn.microsoft.com/en-us/library/12a7a7h3.aspx

已更新-
如果您以Object o = u;(对象的UInt32)进行装箱,则Solution-1和Solution-3中提到的原因同样正确.然后以u = Convert.ToUInt32(o);(UInt32的对象)进行拆箱,然后它将起作用.



One reason I see, it''s does not supporting "u = (UInt32)o;" is - Data type UInt32 is not a CLS-compliant.
http://msdn.microsoft.com/en-us/library/system.uint32.aspx

Below links are also good to understand more on this.
http://msdn.microsoft.com/en-us/library/system.iconvertible.aspx
http://msdn.microsoft.com/en-us/library/12a7a7h3.aspx

Updated -
Reasons mentioned in Solution-1 and Solution-3 are equally correct, if you do Boxing as Object o = u; (UInt32 to object). And then do UnBoxing as u = Convert.ToUInt32(o); (object to UInt32 ) then it will work.

Int32 i = 1;
UInt32 u = (UInt32)i;
Object o = u;
u = Convert.ToUInt32(o);


但是,如果您要将Boxing作为Object o = i;(对象为Int32).然后将Unboxing作为u = Convert.ToUInt32(o);(UInt32的对象)进行操作,则将引发错误,因为进行Boxing时,您使用Boxing时使用的数据类型不匹配.


But if you are doing Boxing as Object o = i; (Int32 to object). And then do UnBoxing as u = Convert.ToUInt32(o); (object to UInt32 ) then it will throw an error, Because the Data type which you used while doing Boxing is not matching while doing Unboxing.


此例外与取消装箱有关.您只能对从中执行装箱的类型执行装箱.

This exception is about Unboxing. You can perform Unboxing only to the type you have performed Boxing from.

i的类型为Int32,要取消装箱的类型为UInt32.

The type of i is Int32 and the type you want to unbox to is UInt32.

您可以将示例的最后一行更改为:

You can change the last line of the example to:

u = (UInt32)(Int32)o;

此行执行从装箱的Int32Int32的拆箱,然后执行从Int32UInt32的投射.

This line performs unboxing from a boxed Int32 to Int32 and, then performs casting from Int32 to UInt32.


这篇关于从对象强制转换为UInt32会引发InvalidCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆