Home >Backend Development >C++ >How Can I Determine if a Generic Type Implements a Specific Generic Interface Using Only its Type Name?
Problem:
Consider these type definitions:
public interface IFoo<t> : IBar<t> {} public class Foo<t> : IFoo<t> {}</t></t></t></t>
Objective:
How to determine if the type Foo
Answer:
Utilizing the approach proposed by TcKs, we can employ the following LINQ query:
bool isBar = foo.GetType().GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IBar));
This query checks whether any of the implemented interfaces of the foo variable is a generic type and its generic type definition matches that of IBar
The above is the detailed content of How Can I Determine if a Generic Type Implements a Specific Generic Interface Using Only its Type Name?. For more information, please follow other related articles on the PHP Chinese website!