2009-07-26 2 views

답변

14

그것은 당신이 필요에 따라, 일지 어떨지를, 아마도 :

bool stringIsObj = typeof(object).IsAssignableFrom(typeof(string)); 

또는 IsSubclassOf :

bool stringIsObj = typeof(string).IsSubclassOf(typeof(object)); 
+1

다른 점은 무엇입니까? – Hugo

+4

예. 인터페이스는 다르게 작동합니다 (하위 클래스는 아님). Foo는 Foo에서 할당 할 수 있지만 Foo는 Foo의 하위 클래스가 아닙니다. (IsSubclassOf는 ** strict ** 하위 클래스입니다.) –

+0

예 : bool stringIsComparable = typeof (IComparable) .IsAssignableFrom (typeof (string)); –

2

대해서 typeof를 (부모 클래스) .IssignableFrom (typeof (ChildClass))

1

나는 당신에게 바다를 제안합니다. 이에 상응하는 rch. 대신 "입니다"사용하는 다음과 같은 키워드 :

if(typeof(object) == typeof(class) 
{ ... } 

:하지만

if (object is class) 
{ ... } 

당신은 단순히 이런 두 유형을 비교할 수이 동일하지 않습니다, 키워드는 "" 객체 클래스가 지정된 클래스인지, 클래스의 상속인지, 인터페이스를 구현하는지 등을 결정할 수 있지만 typeof() 등가성은 실제 유형을 비교합니다.

+0

http://stackoverflow.com/questions/1185559/in-c-what-is-the-equivalent-of-is-keyword-but-using-type-objects/1185570#1185570을 참조하십시오. –