当你这样做的GetType(什么是真的发​​生)? [英] What does really happen when you do GetType()?

查看:111
本文介绍了当你这样做的GetType(什么是真的发​​生)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我们所知,C#对象有一个指向其类型,所以,当你调用的GetType()它会检查指针并返回真正的类的一个对象。但是,如果我这样做:

  A objA =新的A(); 
obj对象=(对象)objA;
如果(obj.GetType()== typeof运算(对象)); //这是真实的



但是,这里所发生 obj对象=(对象) objA; ?是否建立某种形式的参考对象,引用 objA ,但有一个类型的指针,以对象,或者是它一个完全新的对象,这恰好是指向相同的属性,字段等为 objA ?当然,你现在可以访问两个对象的时候,就会有不同的类型,但指向相同的数据。如何工作



另一个问题是:是的GetType()保证返回的实际类型的对象呢?例如,假设有一个与签名 void方法(对象发件人)并传递类型的对象 A 作为一种方法的参数。请问 sender.GetType()返回类型 A 对象?为什么?



其他棘手的事情是,你可以做(A)OBJ ,也将努力。如何CLR现在 OBJ 一度键入 A



会很高兴,如果有人能通过CLR C#一样。



更新。我休息不好下来一点再清楚不过了,张贴问题之前应该运行的代码。所以,如果的GetType()真的总是返回真正的类型,比所有其他问题变得清晰了。


解决方案

据我们所知,C#对象有一个指向其类型,所以,当你调用的GetType()它会检查指针并返回真正的类的一个对象。




正确的。




如果我这样做这样的:




 类A {} 
p级
{
酒店的公共静态无效的主要()
{
一个objA =新的A();
obj对象=(对象)objA;
布尔B = obj.GetType()== typeof运算(对象); //这是真实的
}
}

没有,那是假的。试试吧!




但在这里会发生什么obj对象=(对象)objA ;?




这是在objA参考被复制到变量物镜。 (除非是值类型,在这种情况下,它是盒装和盒子的引用复制到OBJ。)




它是否建立某种形式的参考对象,引用objA,但有一个类型的指针对象,或者是一个完全新的对象,这恰好是指向相同的属性,字段等为objA?




都不是。它复制引用,期限。这是完全不变。




当然,你现在可以访问两个对象的时候,就会有不同的类型,但指向相同的数据。这是如何工作的?




它没有。这个问题的前提是假设它是假的。他们不会有不同的类型。它们是相同的参考。在变量的有不同的类型,但是这无关紧要;你不问其类型的变量,你问的变量的内容的为它的类型。




时的GetType()保证返回的实际类型的对象?




有关您的目的,是的。有涉及晦涩难懂的情况COM互操作的地方没有。




例如,假设有一个与签名void方法的方法(对象发件人)我们通过A类的对象作为参数。将sender.GetType()返回类型,或对象?




A型。




和为什么




由于是这样的对象的类型。




其他棘手的事情是你可以做的(A)目标文件,它会工作。如何CLR现在OBJ曾经是A型的?




C#编译器生成一个castclass指令。该castclass指令做了运行时检查,以验证对象引用实现所需的类型。如果它不那么CLR会抛出异常。


As we know, C# objects have a pointer to their type, so when you call GetType() it checks that pointer and returns the real type of an object. But if I do this:

A objA = new A();
object obj = (object)objA;
if (obj.GetType() == typeof(object)) ; // this is true

But what happens here object obj = (object)objA;? Does it create some sort of reference object, that references objA, but has a type pointer to object, or is it a completely new object, that just happens to be pointing to the same properties, fields, etc. as objA? Certainly, you can access both objects now and they will have a different type, but point to the same data. How does that work?

The other question is: is GetType() guaranteed to return the actual type of an object? For instance, say there is a method with signature void Method(object sender) and we pass object of type A as a parameter. Will sender.GetType() return type A, or object? And Why?

Other tricky thing is that you can do (A)obj and it will work. How does CLR now that obj was once of type A?

Would be glad if someone could break it down a bit clearer than "C# via CLR" does.

Update. My bad, should have run the code before posting the question. So, if GetType() really always returns the real type, than all other questions become clear too.

解决方案

As we know, C# objects have a pointer to their type, so when you call GetType() it checks that pointer and returns the real type of an object.

Correct.

if I do this:

class A {}
class P
{
    public static void Main()
    {
        A objA = new A(); 
        object obj = (object)objA; 
        bool b = obj.GetType() == typeof(object) ; // this is true
    }
}

No, that's false. Try it!

But what happens here object obj = (object)objA;?

The reference that is in objA is copied to the variable obj. (Unless A is a value type, in which case it is boxed and a reference to the box is copied to obj.)

Does it create some sort of reference object, that references objA, but has a type pointer to object, or is it a completely new object, that just happens to be pointing to the same properties, fields, etc. as objA?

Neither. It copies the reference, period. It is completely unchanged.

Certainly, you can access both objects now and they will have a different type, but point to the same data. How does that work?

It doesn't. The question is predicated on an assumption which is false. They will not have different types. They are the same reference. The variables have different types, but that's irrelevant; you're not asking the variable for its type, you're asking the contents of the variable for its type.

is GetType() guaranteed to return the actual type of an object?

For your purposes, yes. There are obscure situations involving COM interop where it does not.

For instance, say there is a method with signature void Method(object sender) and we pass object of type A as a parameter. Will sender.GetType() return type A, or object?

Type A.

And Why?

Because that's the type of the object.

Other tricky thing is that you can do (A)obj and it will work. How does CLR now that obj was once of type A?

The C# compiler generates a castclass instruction. The castclass instruction does a runtime check to verify that the object reference implements the desired type. If it does not then the CLR throws an exception.

这篇关于当你这样做的GetType(什么是真的发​​生)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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