在c#中转换对象类型

时间:2010-10-12 13:30:57

标签: c# casting

我今天遇到了一个问题,我不完全确定为什么它不起作用。

以下代码示例将崩溃:

static void Main(string[] args)
{
     int i32 = 10;
     object obj = i32;
     long i64 = (long)obj;
}

这将导致InvalidCastException。为什么这不起作用? C#不够聪明,不知道该对象实际上是int类型吗?

我已经提出了一个解决方法,但我很好奇为什么上面的代码示例首先不起作用。

谢谢, 添

3 个答案:

答案 0 :(得分:9)

从盒装Int32到Int64没有可用的强制转换。 对int进行中间转换应该有效,因为编译器愿意生成这个:

// verify obj is a boxed int, unbox it, and perform the *statically*
// known steps necessary to convert an int to a long
long i64 = (long) ((int)obj);

但不是(假设)这个:

// Find out what type obj *actually* is at run-time and perform 
// the-known-only-at-run-time steps necessary to produce  
// a long from it, involving *type-specific* IL instructions  
long i64 = (long)obj; 

这是Eric Lippert对此的blog post

答案 1 :(得分:3)

查看Eric Lippert的this blog post了解详情。

它的要点是编译器弄清楚(通过反复试验,因为object可以是任何东西)已经装箱的类型以及它是否可以是安全演员。

答案 2 :(得分:1)

您的意思是编译器还是运行时?

运行时足够智能,因此会抛出InvalidCastException。然而,编译器无法确定您的对象是什么类型,因为您装箱了你的int。

  

装箱和拆箱启用值类型   被视为对象。拳击a   value类型将其打包在一个   Object引用的实例   类型。

因此,由于它被装箱作为对象,编译器不会抱怨它。

在此处查看有关装箱和拆箱的更多信息:

http://msdn.microsoft.com/en-us/library/yz2be5wk%28VS.80%29.aspx

相关问题