【发布时间】:2020-02-21 11:15:49
【问题描述】:
我有一个通用函数,如下所示
private List<T> GetAll<T>()
{
var listOfTModels = // gets the list of T from the database.
return listOfTModels;
}
我需要根据将在运行时决定的字符串将模型 ( T ) 动态传递给此函数。
public void SomeFunction(string modelName)
{
// Call the above Get method with modelName parameter as the 'T'
var listOfModels = GetAll<something>(); //not sure what type this something should be "Type" or "string"
// Further Logic on listOfModels
}
这是怎么做到的?
【问题讨论】:
-
在 DbContext 上有一个方法
Set<T>(),它将返回一个 T 类型的 DbSet。您可以使用反射获得T