Will GetType() return the most derived type when called from the base class?
Asked Answered
I

4

137

Will GetType() return the most derived type when called from the base class?

Example:

public abstract class A
{
    private Type GetInfo()
    {
         return System.Attribute.GetCustomAttributes(this.GetType());
    }
}

public class B : A
{
   //Fields here have some custom attributes added to them
}

Or should I just make an abstract method that the derived classes will have to implement like the following?

public abstract class A
{
    protected abstract Type GetSubType();

    private Type GetInfo()
    {
         return System.Attribute.GetCustomAttributes(GetSubType());
    }
}

public class B : A
{
   //Fields here have some custom attributes added to them

   protected Type GetSubType()
   {
       return GetType();
   }
}
Ias answered 25/4, 2011 at 16:37 Comment(3)
well - did you try it out?Grain
@Grain normally I would just do that but I am not at a computer ... just used my phone to make the post because a solution for a problem was starting to form and I was curious to know now! =PIas
Related: “this” keyword type when called on an object of derived class from base classGandy
H
157

GetType() will return the actual, instantiated type. In your case, if you call GetType() on an instance of B, it will return typeof(B), even if the variable in question is declared as a reference to an A.

There is no reason for your GetSubType() method.

Harbaugh answered 25/4, 2011 at 16:40 Comment(2)
Does it though? I am passing an instance of an object from its (abstract) super class, the receiver only sees the super class. Is it not that it will always return what the handle was defined as not the instance? - or am i missing something?Vitellin
Ah ha.. the difference is using TypeOf(X) vs. x.GetType() i thinkVitellin
C
25

GetType always returns the type that was actually instantiated. i.e. the most derived type. This means your GetSubType behaves just like GetType itself and thus is unnecessary.

To statically get the type information of some type you can use typeof(MyClass).

Your code has a mistake though: System.Attribute.GetCustomAttributes returns Attribute[] not Type.

Chickabiddy answered 25/4, 2011 at 16:41 Comment(0)
M
12

GetType always returns the actual type.

The reason for it is deep in the .NET framework and CLR, as the JIT and CLR use the .GetType method to create a Type object in memory that holds the information on the object, and all access to the object and compilation are via this Type instance.

For more information, take a look in the book "CLR via C#" from Microsoft Press.

Milkman answered 7/8, 2013 at 18:9 Comment(0)
C
1

Output:

GetType:
        Parent: 'Playground.ParentClass'
        Child: 'Playground.ChildClass'
        Child as Parent: 'Playground.ChildClass'

GetParentType:
        Parent: 'Playground.ParentClass'
        Child: 'Playground.ParentClass'
        Child as Parent: 'Playground.ParentClass'

Program.cs:

using Playground;

var parent = new ParentClass();
var child = new ChildClass();
var childAsParent = child as ParentClass;

Console.WriteLine("GetType:\n" +
                  $"\tParent: '{parent.GetType()}'\n" +
                  $"\tChild: '{child.GetType()}'\n" +
                  $"\tChild as Parent: '{childAsParent.GetType()}'\n");

Console.WriteLine("GetParentType:\n" +
                  $"\tParent: '{parent.GetParentType()}'\n" +
                  $"\tChild: '{child.GetParentType()}'\n" +
                  $"\tChild as Parent: '{childAsParent.GetParentType()}'\n");

ChildClass.cs

namespace Playground
{
    public class ChildClass : ParentClass
    {
    }
}

ParentClass.cs

namespace Playground
{
    public class ParentClass
    {
        public Type GetParentType()
        {
            return typeof(ParentClass);
        }
    }
}
Christner answered 5/6, 2022 at 16:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.