如何是ValueType.GetType()能够确定结构的类型? [英] How is ValueType.GetType() able to determine the type of the struct?

查看:214
本文介绍了如何是ValueType.GetType()能够确定结构的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关引用类型,该对象的内存布局

  |类型对象的指针|
|同步座|
|实例字段... |
 

有关值类型,所述对象的布局似乎是

  |实例字段... |
 

有关引用类型,的GetType意味着找到从类型对象的指针的对象。一个给定的参考类型的对象点的所有对象与同一类型的对象(其中也有方法表)

有关值类型,该指针将不可用。那么,如何的GetType()的工作?

我检查了谷歌,我发现这个片段..这是一个有点朦胧。有人能详细点吗?

  

的解决方案是,在位置   它的值存储始得店   某种类型的值。这是   由验证保证。   来源

解决方案

调用的GetType()上的值类型框,值类型。通过移动值类型到堆上你现在有现在有一个指向对象的类型引用类型。

如果你想避免拳击时,你可以叫<一href="http://msdn.microsoft.com/en-us/library/system.type.gettype$c$c.aspx"><$c$c>GetType$c$c它返回一个枚举,指示值类型的类型,而拳击吧。

下面是一个例子,显示了发生在拳击:

C#:

 类节目
{
    静态无效的主要()
    {
    34.GetType();
    }
}
 

IL为主要()

 。方法私人hidebysig静态无效的主要()CIL管理
{
    	。入口点
    .maxstack 8
    L_0000:ldc.i4.s输入0​​x22
    L_0002:箱INT32
    L_0007:调用实例类[mscorlib程序]的System.Type [mscorlib程序] System.Object的::的GetType()
    L_000c:流行
    L_000d:RET
}
 


编辑::要显示什么编译器是干什么的,让改变文字的类型是这样的:

 类节目
{
    静态无效的主要()
    {
    34L.GetType();
    }
}
 

添加L System.Int64 。编译器看到这一点,当它发射指令,它看起来是这样的:

 。方法私人hidebysig静态无效的主要()CIL管理
{
        。入口点
        .maxstack 8
        L_0000:ldc.i4.s输入0​​x22
        L_0002:conv.i8
        L_0003:箱的Int64
        L_0008:调用实例类[mscorlib程序]的System.Type [mscorlib程序] System.Object的::的GetType()
        L_000d:流行
        L_000e:RET
}
 

正如你所看到的,编译器做决定的正确指导的辛勤工作发出后,它要由CLR执行它们。

For a reference type, the object's memory layout is

| Type Object pointer|
|    Sync Block      |
|  Instance fields...|

For a value type, the object layout seems to be

|  Instance fields...|

For a reference type, GetType means find the object from the 'Type Object pointer'. All objects of a given reference type object point to the same type object (which also has the method table)

For a value type, this pointer isn't available. So how does GetType() work ?

I checked with Google and I found this snippet.. which is a bit hazy. Can someone elaborate?

The solution is that the location in which a value is stored may only store values of a certain type. This is guaranteed by the verifier. Source

解决方案

Calling GetType() on a value type boxes that value type. By moving the value type onto the heap you now have a reference type which now has a pointer to the type of that object.

If you wish to avoid boxing you can call GetTypeCode which returns an enumeration that indicates the type of the value type without boxing it.

Here is an example showing the boxing that takes place:

C#:

class Program
{
    static void Main()
    {
    	34.GetType();
    }
}

IL for Main():

.method private hidebysig static void Main() cil managed
{
    	.entrypoint
    	.maxstack 8
    	L_0000: ldc.i4.s 0x22
    	L_0002: box int32
    	L_0007: call instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()
    	L_000c: pop 
    	L_000d: ret 
}


Edit: To show what the compiler is doing, lets change the type of the literal like this:

class Program
{
    static void Main()
    {
    	34L.GetType();
    }
}

By adding the "L" after the literal I am telling the compiler that I want this literal to be converted to a System.Int64. The compiler sees this and when it emits the box instruction it looks like this:

.method private hidebysig static void Main() cil managed
{
        .entrypoint
        .maxstack 8
        L_0000: ldc.i4.s 0x22
        L_0002: conv.i8 
        L_0003: box int64
        L_0008: call instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()
        L_000d: pop 
        L_000e: ret 
}

As you can see, the compiler has done the hard work of determining the correct instructions to emit, after that it is up to the CLR to execute them.

这篇关于如何是ValueType.GetType()能够确定结构的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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