如何确定是否一种类型的实现特定的通用的接口类型

如何确定是否一种类型的实现特定的通用的接口类型

问题描述:

假设以下类型定义:

public interface IFoo<T> : IBar<T> {}
public class Foo<T> : IFoo<T> {}

如何才能知道是否该类型实现了通用接口伊巴尔&LT; T&GT; 只有当错位类型可用?

How do I find out whether the type Foo implements the generic interface IBar<T> when only the mangled type is available?

通过使用答案从第三文化孩子也可以用下面的LINQ查询完成的:

By using the answer from TcKs it can also be done with the following LINQ query:

bool isBar = foo.GetType().GetInterfaces().Any(x =>
  x.IsGenericType &&
  x.GetGenericTypeDefinition() == typeof(IBar<>));