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

前端 未结 3 1417
孤城傲影
孤城傲影 2020-12-23 23:55

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

Example:

public abstract class A
{
    private Type GetInfo()
    {
             


        
3条回答
  •  春和景丽
    2020-12-24 00:47

    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.

提交回复
热议问题