Reputation: 1120
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?
Upvotes: 3
Views: 231
Reputation: 7054
The DataNamesMapper
is open type. To make it closed to T
you need to use this syntax:
DataNamesMapper<T> mapper = new DataNamesMapper<T>();
Upvotes: 3