C# 如何检查当前类型(类型的对象)是否具有所需的接口(或父类型)

C# 如何检查当前类型(类型的对象)是否具有所需的接口(或父类型),c#,.net,types,C#,.net,Types,我有一个some类型(类型为的对象)。需要检查此类型是否具有接口IList 我如何做到这一点?您可以使用该方法 我认为最简单的方法是使用 从你的例子来看: Type customListType = new YourCustomListType().GetType(); if (typeof(IList).IsAssignableFrom(customListType)) { //Will be true if "YourCustomListType : IList" } 假设您有一

我有一个some类型(类型为
的对象
)。需要检查此类型是否具有接口IList
我如何做到这一点?

您可以使用该方法


我认为最简单的方法是使用

从你的例子来看:

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

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

假设您有一个对象
type
,其类型为
System.type
(我从OP收集的内容)


您可以使用
is
检查:

MyType obj = new MyType();
if (obj is IList)
{
  // obj implements IList
}
Type type = ...;
typeof(IList).IsAssignableFrom(type)
MyType obj = new MyType();
if (obj is IList)
{
  // obj implements IList
}