How to determine if a type implements a specific generic interface type
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C sharp reflection scenarios, you often need to know whether a runtime type implements a generic interface such as IRepository<T> or IHandle<TCommand>. Direct type equality checks are not enough because concrete implementations are closed generic variants. The correct approach is to inspect implemented interfaces and compare generic type definitions.
Closed Versus Open Generic Interfaces
Given a generic interface:
A concrete type might implement IRepository<Customer>. At runtime, you typically want to ask whether it implements IRepository<> for any type argument.
Reflection Helper Method
Use a reusable helper that handles both direct and inherited interfaces.
Usage:
Example Types
Checks:
This works regardless of concrete type argument.
Matching a Specific Closed Generic Interface
If you need exact closed type match, compare directly.
Use open-generic check for category matching, closed-generic check for exact contract matching.
Handling Generic Base Classes
Sometimes interfaces are inherited through base classes. GetInterfaces includes inherited interfaces, so helper still works. If you also need generic base-class matching, add BaseType traversal logic separately.
This complements interface checks in plugin-style systems.
Performance Considerations
Reflection can be expensive in hot paths. Cache results when checking many types repeatedly.
Caching makes startup scanning and DI container registrations faster.
Dependency Injection Registration Example
This check is useful when auto-registering handlers in a dependency injection container.
This pattern helps build plugin-like systems where implementations are discovered at runtime. It is also common in mediator-style command pipelines and message handlers. Reflection filters simplify modular architecture bootstrapping. They improve startup clarity.
Testing Reflection Helpers
Unit tests should include:
- Type directly implementing interface.
- Type inheriting implementation from base class.
- Type implementing unrelated interface.
- Invalid input where interface argument is not open generic.
This keeps helper behavior stable as codebase evolves.
Common Pitfalls
- Comparing interface type directly to open generic definition. Fix by calling
GetGenericTypeDefinitionon implemented interfaces. - Passing closed generic type when helper expects open interface type. Fix by validating
IsGenericTypeDefinition. - Ignoring inherited interfaces. Fix by using
GetInterfaces, not only direct declarations. - Running reflection checks repeatedly without cache. Fix by memoizing results in concurrent dictionary.
- Mixing class inheritance checks with interface checks. Fix by handling generic base classes separately.
Summary
- Use reflection to inspect implemented interfaces and compare open generic definitions.
- Open generic checks answer category questions such as
IRepository<>support. - Closed generic checks answer exact contract questions.
- Add caching when checks run frequently.
- Validate helper inputs to avoid silent false negatives.

