상세 컨텐츠

본문 제목

SubClass 혹은 BaseClass 체크하기 ( System.Type )

C#

by 탑~! 2018. 9. 6. 12:26

본문

해당 클래스가 어떤 클래스를 상속받은 서브 클래스인지 확인하는 방법이다.
System.Type 에 들어있으며 Type 값으로 체크한다.


typeof
(SubClass).IsSubclassOf(typeof (BaseClass)); // returns true
typeof (BaseClass).IsSubclassOf(typeof (BaseClass)); // returns false


위와는 반대로 BaseClass 로 체크할때에는
if (typeof(BaseClass).IsAssignableFrom(typeof(SubClass)))
이렇게 사용하면 된다. 

이 외에서 
GetType(String) 등과 같이 문자열에서 Type 을 뽑아내는 함수 등 
유용한 함수들이 상단수 있다.


자세한 도움말은 아래 참조 http://msdn.microsoft.com/en-us/library/42892f65.aspx



출처: http://flystone.tistory.com/78?category=410122 [MomO]

관련글 더보기