C# 如何找到其泛型类型派生自给定基类型的所有数据库集?

C# 如何找到其泛型类型派生自给定基类型的所有数据库集?,c#,entity-framework,generics,reflection,entity-framework-4.1,C#,Entity Framework,Generics,Reflection,Entity Framework 4.1,如何获取包含类型派生自IncomingServiceOrderBase的所有数据库集的列表 我可以使用反射来获取所有的dbset,但是如何将其过滤到只包含派生类型的dbset呢 上下文 public class MyContext : DbContext { public DbSet<BuildingOrder> BuildingOrders { get; set; } public DbSet<DeliveryOrder> DeliveryOrders

如何获取包含类型派生自IncomingServiceOrderBase的所有数据库集的
列表

我可以使用反射来获取所有的dbset,但是如何将其过滤到只包含派生类型的dbset呢

上下文

public class MyContext : DbContext
{
    public DbSet<BuildingOrder> BuildingOrders { get; set; }
    public DbSet<DeliveryOrder> DeliveryOrders { get; set; }
    public DbSet<RetailAssemblyOrder> RetailAssemblyOrders { get; set; }
}
typeof(BaseType).IsAssignableFrom(DerivedType)
。它将返回真/假。看

要将
DbSet
转换为
T
(以便进行此比较),请获取每个属性的类型并执行如下操作:

    public static Type GetGenericBaseType( this Type Type ) {
        if ( Type == null ) {
            throw new ArgumentNullException( "Type" );
        }
        if ( !Type.IsGenericType ) {
            throw new ArgumentOutOfRangeException( "Type", Type.FullName + " isn't Generic" );
        }
        Type[] args = Type.GetGenericArguments();
        if ( args.Length != 1 ) {
            throw new ArgumentOutOfRangeException( "Type", Type.FullName + " isn't a Generic type with one argument -- e.g. T<U>" );
        }
        return args[0];
    }
var sets =
    from p in typeof(MyContext).GetProperties()
    where p.PropertyType.IsGenericType
    && p.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>)
    let entityType = p.PropertyType.GetGenericArguments().First()
    where typeof(IncomingServiceOrderBase).IsAssignableFrom(entityType)
    select p.Name;
公共静态类型GetGenericBaseType(此类型){
if(Type==null){
抛出新的ArgumentNullException(“类型”);
}
如果(!Type.IsGenericType){
抛出新ArgumentOutOfRangeException(“Type”,Type.FullName+“不是泛型”);
}
Type[]args=Type.GetGenericArguments();
如果(参数长度!=1){
抛出新ArgumentOutOfRangeException(“Type”,Type.FullName+“不是只有一个参数的泛型类型——例如t”);
}
返回参数[0];
}

您可以这样做:

    public static Type GetGenericBaseType( this Type Type ) {
        if ( Type == null ) {
            throw new ArgumentNullException( "Type" );
        }
        if ( !Type.IsGenericType ) {
            throw new ArgumentOutOfRangeException( "Type", Type.FullName + " isn't Generic" );
        }
        Type[] args = Type.GetGenericArguments();
        if ( args.Length != 1 ) {
            throw new ArgumentOutOfRangeException( "Type", Type.FullName + " isn't a Generic type with one argument -- e.g. T<U>" );
        }
        return args[0];
    }
var sets =
    from p in typeof(MyContext).GetProperties()
    where p.PropertyType.IsGenericType
    && p.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>)
    let entityType = p.PropertyType.GetGenericArguments().First()
    where typeof(IncomingServiceOrderBase).IsAssignableFrom(entityType)
    select p.Name;
var集=
从typeof(MyContext.GetProperties()中的p开始
其中p.PropertyType.IsGenericType
&&p.PropertyType.GetGenericTypeDefinition()==typeof(DbSet)
让entityType=p.PropertyType.GetGenericArguments().First()
其中typeof(IncomingServiceOrderBase).IsAssignableFrom(entityType)
选择p.名称;
(这将返回属性的名称;如果需要实际的DbSet实例,请将
p.Name
替换为
p.GetValue(context,null)