相关疑难解决方法(0)

从基类调用时,GetType()是否会返回派生类型最多的类型?

从基类调用时,GetType()是否会返回派生类型最多的类型?

例:

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
}
Run Code Online (Sandbox Code Playgroud)

或者我应该创建一个抽象方法,派生类必须实现如下所示?

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();
   }
}
Run Code Online (Sandbox Code Playgroud)

c# polymorphism inheritance

115
推荐指数
3
解决办法
4万
查看次数

标签 统计

c# ×1

inheritance ×1

polymorphism ×1