6

I am trying to write a method using reflection to return all classes that are subclasses of a class that uses generics, without being limited by the generic type. So for example, in EF I want to find all mapping classes. The classes are setup like:

public class clientMap : EntityTypeConfiguration<Client> {}

I want to find all classes in my assembly that is a subclass of of EntityTypeConfiguration<T>, without specifying Client as T specifically. I want to return the entity type configuration for all classes in my application without hardcoding it.

Without generics I would loop through the types in the assembly, check if type.IsSubclassOf(typeof(BaseClass)), however I am not sure how to do this when dealing with generics.

KallDrexx
  • 27,229
  • 33
  • 143
  • 254
  • 2
    Duplicate? http://stackoverflow.com/questions/457676/c-reflection-check-if-a-class-is-derived-from-a-generic-class – Rick Liddle Jun 21 '11 at 14:40

2 Answers2

10

I believe that you want something like this:

static class TypeExtensions {
    public static bool IsDerivedFromOpenGenericType(
        this Type type,
        Type openGenericType
    ) {
        Contract.Requires(type != null);
        Contract.Requires(openGenericType != null);
        Contract.Requires(openGenericType.IsGenericTypeDefinition);
        return type.GetTypeHierarchy()
                   .Where(t => t.IsGenericType)
                   .Select(t => t.GetGenericTypeDefinition())
                   .Any(t => openGenericType.Equals(t));
    }

    public static IEnumerable<Type> GetTypeHierarchy(this Type type) {
        Contract.Requires(type != null);
        Type currentType = type;
        while (currentType != null) {
            yield return currentType;
            currentType = currentType.BaseType;
        }
    }
}

These tests pass:

class Foo<T> { }
class Bar : Foo<int> { }
class FooBar : Bar { }

[Fact]
public void BarIsDerivedFromOpenGenericFoo() {
    Assert.True(typeof(Bar).IsDerivedFromOpenGenericType(typeof(Foo<>)));
}

[Fact]
public void FooBarIsDerivedFromOpenGenericFoo() {
    Assert.True(typeof(FooBar).IsDerivedFromOpenGenericType(typeof(Foo<>)));
}

[Fact]
public void StringIsNotDerivedFromOpenGenericFoo() {
    Assert.False(typeof(string).IsDerivedFromOpenGenericType(typeof(Foo<>)));
}
jason
  • 236,483
  • 35
  • 423
  • 525
  • What should `openGenericType` be in actual code? `EntityTypeConfiguration<>` or should something be in the brackets? – KallDrexx Jun 21 '11 at 14:49
  • For your case, `typeof(EntityTypeConfiguration<>)`. Note the code and tests make this clear. – jason Jun 21 '11 at 14:51
  • Good style, Contracts is a good approach and its (the namespace) usage should be encouraged. I find this more useful than answers with console application examples. – Jonno May 18 '12 at 06:26
0

as far as I understood your case the following should be enough

type.BaseType != null &&
type.BaseType.MetadataToken == typeof(EntityTypeConfiguration<>).MetadataToken
alx
  • 257
  • 1
  • 2