Determining if an Open Generic Type IsAssignableFrom a Type[source]

    <title>Determining if an Open Generic Type IsAssignableFrom a Type</title>
    <description>How to determine if an open generic type IsAssignableFrom a Type in .NET circa 2011</description>
    <category>Legacy blog posts</category>
<glacius:macro name="legacy blargh banner">
<p>It may come to pass in your dirty .NET dealings that you need to do something like this:</p>
<glacius:code lang="csharp"><![CDATA[public void DoTheAwesome(Type type) {
  if (!typeof(IEnumerable<>).IsAssignableFrom(type)) {
  // do awesome stuff
  If you're asking why you might need to ever do something like this, you clearly haven't 
  worked with enough Inversion of Control frameworks.
  The point is that the above conditional is always <code>false</code> no matter the value
  of <code>type</code>. I need it to be <code>true</code> basically if <code>type</code> 
  implements <code>IEnumerable&lt;T&gt;</code>, only it wasn't just <code>IEnumerable&lt;T&gt;</code> 
  and <code>type</code> could be anything.
  So a bit of googling and fast-and-furious copypasting from StackOverflow yielded 
  something that worked but not quite (basically the open generic part didn't work,
  which what I really wanted). But I was able to wrangle it into something that suited 
  my needs. The original code is <a href="">here</a>.
<glacius:code lang="csharp"><![CDATA[public static class AssignableExtensions {
  /// <summary>
  /// Determines whether the <paramref name="genericType"/> is assignable from
  /// <paramref name="givenType"/> taking into account generic definitions
  /// </summary>
  public static bool IsAssignableToGenericType(this Type givenType, Type genericType) {
    if (givenType == null || genericType == null) {
      return false;
    return givenType == genericType 
      || givenType.MapsToGenericTypeDefinition(genericType) 
      || givenType.HasInterfaceThatMapsToGenericTypeDefinition(genericType) 
      || givenType.BaseType.IsAssignableToGenericType(genericType);
  private static bool HasInterfaceThatMapsToGenericTypeDefinition(this Type givenType, Type genericType) {
    return givenType
      .Where(it => it.IsGenericType)
      .Any(it => it.GetGenericTypeDefinition() == genericType);
  private static bool MapsToGenericTypeDefinition(this Type givenType, Type genericType) {
    return genericType.IsGenericTypeDefinition
      && givenType.IsGenericType
      && givenType.GetGenericTypeDefinition() == genericType;
  And here are some tests to verify that it might be doing what you think it might 
  or might not be doing:
<glacius:code lang="csharp"><![CDATA[[TestFixture]
public class AssignableTests {
  public void Should_be_assignable_from_open_generic_type_to_concrete_open_generic_type() {
  public void Should_be_assignable_from_open_generic_type_to_generic_interface_type() {
  public void Should_be_assignable_from_open_generic_type_to_itself() {
  public void Should_be_assignable_from_open_generic_type_to_concrete_generic_type() {
  public void Should_be_assignable_from_open_generic_type_to_nongeneric_concrete_type() {
  public interface IFoo<T> {}
  public class Foo<T> : IFoo<T> { }
  public class Bar : IFoo<int> { }
  This code is licensed under 
  <a href="">Creative Commons</a>
  (same as StackOverflow) so feel free to go to town.