What does really happen when you do GetType()?

后端 未结 1 903
广开言路
广开言路 2020-12-30 14:54

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:

1条回答
  •  生来不讨喜
    2020-12-30 15:43

    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 讨论(0)
提交回复
热议问题