3 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個贊
通過使用TcK的答案,也可以使用以下LINQ查詢來完成:
bool isBar = foo.GetType().GetInterfaces().Any(x =>
x.IsGenericType &&
x.GetGenericTypeDefinition() == typeof(IBar<>));

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個贊
您必須遍歷繼承樹,并在樹中找到每個類的所有接口,如果接口是通用的,則與typeof(IBar<>)調(diào)用結(jié)果進(jìn)行比較。當(dāng)然,這有點(diǎn)痛苦。Type.GetGenericTypeDefinition

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個贊
public interface IFoo<T> : IBar<T> {}
public class Foo : IFoo<Foo> {}
var implementedInterfaces = typeof( Foo ).GetInterfaces();
foreach( var interfaceType in implementedInterfaces ) {
if ( false == interfaceType.IsGeneric ) { continue; }
var genericType = interfaceType.GetGenericTypeDefinition();
if ( genericType == typeof( IFoo<> ) ) {
// do something !
break;
}
}
- 3 回答
- 0 關(guān)注
- 531 瀏覽
添加回答
舉報(bào)