Skip to content

Instantly share code, notes, and snippets.

@kcargile
Created August 5, 2012 21:29
Show Gist options
  • Save kcargile/3267198 to your computer and use it in GitHub Desktop.
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.
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