此代码示例取自此答案https://stackoverflow.com/a/16491759/98706
public static Boolean PurgeDataObject(this IDataObject dataObject, Guid uid)
{
return PurgeDataObjectImpl((dynamic) dataObject, uid);
}
private static Boolean PurgeDataObjectImpl<T>(T dataObject, Guid uid)
where T : IDataObject
{
return DataProvider.DeleteDataObject<T>(uid, DataProvider.GetConnection());
}
我有类似的情况,我想在执行时根据类型返回相关的 EF 实体,以尝试清理一些现有的继承代码,如下所示:(
不同的是我希望函数返回一个IEnumerable<T>
GetFilteredData
不会有一个概念,所以T
我得到了编译错误The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)
private IEnumerable<T> GetFilteredData(Type entityType, SortedList<string, string> filterList, List<int> weeks)
{
return GetFilteredDataImpl((dynamic)entityType, filterList, weeks);
}
private IEnumerable<T> GetFilteredDataImpl<T>(T entityType, SortedList<string, string> filterList, List<int> weeks) where T : EntityObject
{
var data = _modelContext.CreateObjectSet<T>().AsExpandable();
// do more filtering and then call .ToList() to return a List<T>
}