我使用这个简洁的classes将MySQL数据库中的数据表映射到对象中。现在我想写一个泛型函数来返回各种不同类的列表,这样我就可以调用:
List<Person> persons = ReadDataTable<Person>();
List<Car> persons = ReadDataTable<Car>();
还有更多的类。
但是我不理解如何在我的泛型函数中创建对象DataNamesMapper:
public List<T> ReadDataTable<T>()
{
List<T> parsedObjects = new List<T>();
DataNamesMapper<typeof(T)> mapper = new DataNamesMapper<typeof(T)> (); //<- error
DataNamesMapper<Person> mapper = new DataNamesMapper<Person> (); //<- no error, non-generic
//...query data, map and fill list
return parsedObjects;
}
定义了DataMapper:
public class DataNamesMapper<TEntity> where TEntity : class, new()
{
...
}
这个是可能的吗?
发布于 2019-01-30 19:01:26
DataNamesMapper
是开放式的。要关闭对T
的访问,您需要使用以下语法:
DataNamesMapper<T> mapper = new DataNamesMapper<T>();
https://stackoverflow.com/questions/54438866
复制相似问题