Determining if a Type Implements a Specific Generic Interface Type
Suppose you have the following type definitions:
public interface IFoo<T> : IBar<T> {} public class Foo<T> : IFoo<T> {}
Given only the mangled type, how can you determine if the type Foo implements the generic interface IBar
Solution with LINQ Query
As suggested by TcKs, you can utilize the following LINQ query:
bool isBar = foo.GetType().GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IBar<>));
This query checks if any of the implemented interfaces of foo is a generic type that matches the IBar
The above is the detailed content of How Can I Determine if a Type Implements a Specific Generic Interface in C#?. For more information, please follow other related articles on the PHP Chinese website!