I'm using this neat classes to map data tables from a MySQL database into objects. Now I would like to write a generic function to return a list for a variety of different classes so I could call:
List<Person> persons = ReadDataTable<Person>();
List<Car> persons = ReadDataTable<Car>();
and many more classes.
But I don't unterstand how to create the object DataNamesMapper in my generic function:
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 is defined:
public class DataNamesMapper<TEntity> where TEntity : class, new()
{
...
}
Is this possible?
The DataNamesMapper
is open type. To make it closed to T
you need to use this syntax:
DataNamesMapper<T> mapper = new DataNamesMapper<T>();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.