使用IsInterface的属性Type..
public void DoCoolStuff<T>()
{
if(typeof(T).IsInterface)
{
//TODO: Cool stuff...
}
}
Run Code Online (Sandbox Code Playgroud)
如果要约束泛型方法,以便type参数只能是实现某个特定接口的类型,那么您应该执行以下操作:
void YourGenericMethod<T>() where T : IYourInterface {
// Do stuff. T is IYourInterface.
}
Run Code Online (Sandbox Code Playgroud)