2015-06-15 41 views
1

我需要一种方法来过滤我的程序集以获取所有类,从泛型类继承。我找到了一些帖子,但他们最喜欢如何检查的继承(例如How to check if a class inherits another class without instantiating it?Check if a class is derived from a generic class)。但这些帖子并没有帮助我。查找使用反射继承泛型类的类

我所有的类都继承自一个特定的类,称为BaseRepository<T>,但某些类可以继承自InterventionBaseRepository<T>(它也继承自BaseRepository<T>)。我需要找到他们。这是我的代码至今:

var q = from t in Assembly.GetExecutingAssembly().GetTypes() 
     where t.IsClass && 
      (t.Namespace != null && 
       t.Namespace.StartsWith("AgroWeb.Core.Data.Repository")) 
     select t; 

因此,简而言之,上述查询通过命名空间,我需要它们之间找到那些谁InterventionBaseRepository<T>继承得到我的所有类。

必须找到一个类的签名的例子:

public class CityRepository : InterventionBaseRepository<City>, ICityRepository 

而且InterventionBaseRepository声明:

public class InterventionBaseRepository<T> : BaseRepository<T> 
    where T : Entity 

我试图用IsAssignableFrom()在上面的链接的帖子为说像这样的查询:

t.IsAssignableFrom(typeof(InterventionBaseRepository<Entity>)) 
+0

你可以在'IsAssignableFrom'上显示你的尝试吗? –

+3

您是否正确地获取了IsAssignableFrom()?即abstraction.IsAssignableFrom(专精) –

+0

@JamesLucas这也是我的想法。它应该工作。 OP需要显示他的代码。 –

回答

0

It's becaus你正在检查实体而不是城市。如果你想使用协通用参数,然后指定InterventionBaseRepository的界面是这样的:

public interface IInterventionBaseRepository<out T> { } 

然后让你的InterventionBaseRepository实现它。现在回购<城市>可以视为回购<实体>。

所以:

public class InterventionBaseRepository<T> : BaseRepository<T>, IInterventionBaseRepository<T> 
where T : Entity 
{ 
} 
+0

即使“城市:实体”是这种情况(我不好意思,我忘了提及它)? – DontVoteMeDown

0

this post

  CityRepository value = new CityRepository(); 
      Type type = value.GetType(); 
      Type baseType = type.BaseType; 
      if (baseType.IsGenericType) 
      { 
       Console.WriteLine(baseType.Name + " is generic type"); 

       Type itemType = baseType.GetGenericArguments()[0]; 
       Console.WriteLine("Generic argument: " + itemType.Name); 

       Type genericType = baseType.GetGenericTypeDefinition(); 
       Console.WriteLine("Generic type: " + genericType.Name); 

       Console.WriteLine("Generic base type: " + genericType.BaseType.Name); 
      } 

      if (type.IsAssignableToGenericType(typeof(BaseRepository<>))) 
      { 
       Console.WriteLine("IsAssignableToGenericType: BaseRepository<>"); 
      } 

     if (type.IsAssignableToGenericType(typeof(InterventionBaseRepository<>))) 
     { 
      Console.WriteLine("IsAssignableToGenericType: InterventionBaseRepository<>"); 
     } 

和:

public static class Utils 
{ 
    public static bool IsAssignableToGenericType(this Type givenType, Type genericType) 
    { 
     var interfaceTypes = givenType.GetInterfaces(); 

     foreach (var it in interfaceTypes) 
     { 
      if (it.IsGenericType && it.GetGenericTypeDefinition() == genericType) 
       return true; 
     } 

     if (givenType.IsGenericType && givenType.GetGenericTypeDefinition() == genericType) 
      return true; 

     Type baseType = givenType.BaseType; 
     if (baseType == null) return false; 

     return IsAssignableToGenericType(baseType, genericType); 
    } 
} 

输出:

InterventionBaseRepository`1 is generic type 
Generic argument: City 
Generic type: InterventionBaseRepository`1 
Generic base type: BaseRepository`1 
IsAssignableToGenericType: BaseRepository<> 
IsAssignableToGenericType: InterventionBaseRepository<> 
+0

请注意,OP链接到非常类似的代码,并声称它没有在他们的情况下工作...(http://stackoverflow.com/questions/457676/check-if-a-class-is-derived-from-a-通用类) –

+0

@AlexeiLevenkov我检查了我的代码,它与代表OP给出的场景的模型类一起工作。它应该工作,如果他检查:var q =从t in Assembly.GetExecutingAssembly()。GetTypes() 其中t.IsClass && (t.Namespace!= null && t.Namespace.StartsWith(“AgroWeb.Core.Data .Repository“))&& t.IsAssignableToGenericType(typeof(InterventionBaseRepository <>)) select t; – Arie

+0

你的代码看起来很好,因为只有一种方法可以实现看起来像问题的目标。这个说明是关于OP已经意识到这种方法,并且不知何故无法让它工作以实现实际的,可能略有不同的目标。 –