开发者

What does really happen when you do GetType()?

开发者 https://www.devze.com 2023-01-08 17:33 出处:网络
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:

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 paramete开发者_高级运维r. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消