Search code examples
c#.netgenericstypesreflection

How to determine if a type implements a specific generic interface type


Assume the following type definitions:

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

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


Solution

  • 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<>));