如何在CLR知道的盒装对象的类型? [英] How does the CLR know the type of a boxed object?
问题描述
当值类型是盒装,它被放在一个非类型化参考对象内。 ?
那么是什么原因在这里无效强制转换异常
长L = 1;
obj对象=(对象)升;
双D =(双)OBJ;
没有,它不是放在一个无类型对象。对于每一个值类型,有一个在CLR装箱引用类型。所以,你得的的东西的,如:
公共类BoxedInt32 //而不是实际的名称
{
私人只读int值;
公共BoxedInt32(int值)
{
THIS.VALUE =价值;
}
}
这盒装的类型不是在C#中直接访问,虽然它在C ++ / CLI。显然,知道原始类型。因此,在C#中,你必须有对象
的变量,但并不意味着这就是实际类型的对象。
$ b
查看 ECMA CLI规范或的通过更多细节C# CLR。
When a value type is boxed, it is placed inside an untyped reference object. So what causes the invalid cast exception here?
long l = 1;
object obj = (object)l;
double d = (double)obj;
No, it's not placed in an untyped object. For each value type, there's a boxed reference type in the CLR. So you'd have something like:
public class BoxedInt32 // Not the actual name
{
private readonly int value;
public BoxedInt32(int value)
{
this.value = value;
}
}
That boxed type isn't directly accessible in C#, although it is in C++/CLI. Obviously that knows the original type. So in C# you have to have a compile-time type of object
for the variable but that doesn't mean that's the actual type of the object.
See the ECMA CLI spec or CLR via C# for more details.
这篇关于如何在CLR知道的盒装对象的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!