2009-08-04 36 views

回答

13

假設你有一個類型System.Type(我從OP聚集)的對象type

Type type = ...; 
typeof(IList).IsAssignableFrom(type) 
+0

+1這回答了我的問題! – IAbstract 2010-02-03 23:28:59

7

您可以使用Type.GetInterface方法。

if (object.GetType().GetInterface("IList") != null) 
{ 
    // object implements IList 
} 
3

我認爲最簡單的方法是使用IsAssignableFrom

從你的例子

所以:

Type customListType = new YourCustomListType().GetType(); 

if (typeof(IList).IsAssignableFrom(customListType)) 
{ 
    //Will be true if "YourCustomListType : IList" 
} 
0

您可以使用is檢查:

MyType obj = new MyType(); 
if (obj is IList) 
{ 
    // obj implements IList 
}