C# Find out是实现接口的泛型类型

C# Find out是实现接口的泛型类型,c#,generics,C#,Generics,在下面的方法中,有没有一种方法可以知道类型T是否实现了特定的接口IMyInterface2 public IList<T> MyMethod<T>() where T : class, IMyInterface1 { return myResult; } Myclass实现了IMyInterface1和非IMyInterface2确保存在: MyMethod<MyClassB>(); public IList<T> MyMethod&l

在下面的方法中,有没有一种方法可以知道类型
T
是否实现了特定的接口
IMyInterface2

public IList<T> MyMethod<T>() where T : class, IMyInterface1
{

   return myResult;
}
Myclass实现了
IMyInterface1
IMyInterface2
确保存在:

MyMethod<MyClassB>();
public IList<T> MyMethod<T>() where T : class, IMyInterface1
{
    if (typeof(IMyInterface2).IsAssignableFrom(typeof(T)))
    {
        // code here
    }

    return myResult;
}
public IList MyMethod(),其中T:class,IMyInterface1
{
if(typeof(IMyInterface2).IsAssignableFrom(typeof(T)))
{
//代码在这里
}
返回我的结果;
}
确保有:

public IList<T> MyMethod<T>() where T : class, IMyInterface1
{
    if (typeof(IMyInterface2).IsAssignableFrom(typeof(T)))
    {
        // code here
    }

    return myResult;
}
public IList MyMethod(),其中T:class,IMyInterface1
{
if(typeof(IMyInterface2).IsAssignableFrom(typeof(T)))
{
//代码在这里
}
返回我的结果;
}

与任何其他对象一样,除了必须使用
typeof
而不是
.GetType()


与任何其他对象一样,除了必须使用
typeof
而不是
.GetType()

var implements = typeof(IMyInterface2).IsAssignableFrom(typeof(T));