如何找到其泛型类型派生自给定基类型的所有DbSet?

时间:2012-05-08 00:18:00

标签: c# entity-framework generics reflection entity-framework-4.1

如何从List获取所包含类型的所有DbSet中的IncomingServiceOrderBase

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

上下文

public class MyContext : DbContext
{
    public DbSet<BuildingOrder> BuildingOrders { get; set; }
    public DbSet<DeliveryOrder> DeliveryOrders { get; set; }
    public DbSet<RetailAssemblyOrder> RetailAssemblyOrders { get; set; }
}

模型

public class BuildingOrder : IncomingManufacturedProductOrderBase { }
public class DeliveryOrder : IncomingServiceOrderBase { }
public class RetailAssemblyOrder : IncomingServiceOrderBase { }

2 个答案:

答案 0 :(得分:20)

您可以这样做:

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;

(这将返回属性的名称;如果您想要实际的DbSet实例,请将p.Name替换为p.GetValue(context, null)

答案 1 :(得分:2)

typeof(BaseType).IsAssignableFrom(DerivedType)。它会返回true / false。见http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom.aspx

要将DbSet<T>转换为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];
    }