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
}
아니면 파생 클래스가 다음과 같이 구현해야하는 추상 메서드를 만들어야합니까?
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();
   }
}
답변
GetType()실제 인스턴스화 된 유형을 반환합니다. 귀하의 경우에는, 당신이 호출하는 경우 GetType()의 예에 B, 그것은 반환 typeof(B)문제의 변수가 참조로 선언 된 경우에도, A.
당신의 GetSubType()방법에 대한 이유가 없습니다 .
답변
GetType항상 실제로 인스턴스화 된 유형을 반환합니다. 즉 가장 파생 된 유형입니다. 이것은 당신이 자신 GetSubType처럼 행동 GetType하므로 불필요 하다는 것을 의미합니다 .
어떤 유형의 유형 정보를 정적으로 얻으려면을 사용할 수 있습니다 typeof(MyClass).
하지만 코드에 오류가 있습니다. System.Attribute.GetCustomAttributesreturns Attribute[]not Type.