如何在实体框架中将字符串作为类型DbSet的类型参数传递?

时间:2019-08-19 06:14:02

标签: c# entity-framework reflection

考虑以下示例代码,将C#泛型类型作为参数传递的最简洁方法是什么?

public dynamic MyDbSet(string typeName)
{
    var typeofDbSet = typeof(DbSet<>);
    Type[] typeArgs = { Type.GetType(typeName) };
    var type = typeofDbSet.MakeGenericType(typeArgs);
    dynamic dbsetOfT = Activator.CreateInstance(type);

    return dbsetOfT;

    //Call it
    //var notificationTypes = MyDbSet("NotificationType");
    //var list = notificationTypes.ToList();
}

或者类似这样的东西:

public dynamic MyDbSet2(string typeName)
{
    var keyValuePairs = new Dictionary<string, dynamic>
    {
        {nameof(NotificationType), Set<NotificationType>().AsQueryable()}
    };

    return keyValuePairs[typeName];
    //Call it
    //var notificationTypes = MyDbSet2("NotificationType");
    //var list = notificationTypes.ToList();
}

2 个答案:

答案 0 :(得分:1)

Activator.CreateInstance(type)将失败,因为DbSet没有公共的无参数构造函数。
除非您确实需要将类型名称作为字符串传递,否则执行此操作的最佳方法是创建泛型函数,获取构造函数并调用它。会是这样的:

public DbSet<T> MyDbSet<T>() where T : class
{
    return (DbSet<T>)typeof(DbSet<T>).GetConstructor(BindingFlags.NonPublic |
       BindingFlags.Instance, null, Type.EmptyTypes, null).Invoke(null);
}

然后,您将通过MyDbSet<NotificationType>()

进行调用

更新
由于必须传递名称,因此您可以执行以下操作:

public dynamic MyDbSet(string typeName)
{
    return typeof(DbSet<>).MakeGenericType(Type.GetType(typeName)).
        GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
        null, Type.EmptyTypes, null).Invoke(null);
}

然后,您将通过MyDbSet("<namespace>.NotificationType")进行调用。需要指定名称空间,因为否则Type.GetType将找不到类型并返回null

答案 1 :(得分:1)

尝试使用此扩展代码:

//Use var list = _context.MyDbSet("ConsoleAppEF.Student").ToList();
public static IQueryable MySet(this SchoolContext context, string typeName)
{
    var T = Type.GetType(typeName);
    // Get the generic type definition
    MethodInfo method = typeof(SchoolContext)
        .GetMethod("MySet", BindingFlags.Public | BindingFlags.Instance);

    // Build a method with the specific type argument you're interested in
    method = method.MakeGenericMethod(T);

    return method.Invoke(context, null) as IQueryable;
}

public static IQueryable<T> Set<T>(this SchoolContext context)
{
    // Get the generic type definition 
    MethodInfo method = typeof(SchoolContext)
        .GetMethod(nameof(SchoolContext.Set), BindingFlags.Public | BindingFlags.Instance);

    // Build a method with the specific type argument you're interested in 
    method = method.MakeGenericMethod(typeof(T));

    return method.Invoke(context, null) as IQueryable<T>;
}

public static IList ToList1(this IQueryable query)
{
    var genericToList = typeof(Enumerable).GetMethod("ToList")
        .MakeGenericMethod(new Type[] { query.ElementType });
    return (IList)genericToList.Invoke(null, new[] { query });
}

示例DbContext:

public class SchoolContext : DbContext
{

    public SchoolContext() : base("SchoolContext")
    {

    }

    public DbSet<Student> Students { get; set; }

    public virtual new DbSet<TEntity> MySet<TEntity>() where TEntity : BaseEntity
    {
        return base.Set<TEntity>();
    }
}

示例实体:

public class Student : BaseEntity
{
    public string LastName { get; set; }
    public string FirstMidName { get; set; }
}

public class BaseEntity
{
    public int Id { get; set; }
}

使用它:

class Program
{
    static void Main(string[] args)
    {
        Seed();
        var _context = new SchoolContext();
        var list = _context.MySet("ConsoleAppEF.Student").ToList1();
    }

    private static void Seed()
    {
        var _context = new SchoolContext();
        var students = new List<Student>
        {
            new Student{FirstMidName="Carson",LastName="Alexander"},
        };

        students.ForEach(s => _context.Students.Add(s));
        _context.SaveChanges();
    }
}