8

如何获取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 { }
4

2 回答 2

23

你可以这样做:

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.Namep.GetValue(context, null)

于 2012-05-08T00:59:25.277 回答
2

typeof(BaseType).IsAssignableFrom(DerivedType). It'll return true/false. See http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom.aspx

To turn DbSet<T> into T (so you can do this comparison) take each property's type and do something like this:

    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];
    }
于 2012-05-08T00:52:35.983 回答