Created
August 5, 2012 21:29
-
-
Save kcargile/3267198 to your computer and use it in GitHub Desktop.
.NET get a list of types that implement a (non-specific) generic interface.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
namespace Reflection | |
{ | |
public interface IGenericInterface<T> | |
{ | |
} | |
public static class ReflectionUtil | |
{ | |
public static IEnumerable<Type> GetTypesHavingGenericInterface(Assembly assembly) | |
{ | |
if (null == assembly) | |
{ | |
throw new ArgumentNullException("assembly"); | |
} | |
return | |
from type in assembly.GetTypes() | |
where | |
type.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IGenericInterface<>)) && | |
type.IsClass | |
select type; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment