CipherIS
asked on
C# Implement Generics on Export Class
Trying to create an export using Generics. Most of it works except one piece of code. Basically I want to pass the model to the export and perform the export in the class. Here is what I have (error is indicated below).
private void Form1_Load(object sender, EventArgs e)
{
SQLData<PersonModel> sqldata = new SQLData<PersonModel>();
List<PersonModel> personModel = sqldata.SelectSPROC("Get_Persons");
ExportPersons.PerformExport<PersonModel>();
}
public static class ExportPersons
{
public static void PerformExport<T>()
{
ExportExcel<T> exportExcel = new ExportExcel<T>();
exportExcel.ExportData<T>();
}
}
public class ExportExcel<T> : IExportData<T>
{
public void ExportData<T>()
{
var model = List<T>(); //HERE IS THE ERROR - 'System.Collections.Generic.List<T>' is a 'type' but is used like a 'variable'
}
}
public interface IExportData<T>
{
void ExportData<T>(); //HERE - Type parameter 'T' has the same name as the type parameter for the outer type.
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked. Had to make some changes to the code but it worked. Wish I had Resharper on this computer.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I have no compiler at hands right now, but this should be the outline:
public interface IModel<T> { IEnumerable<T> GetAll(); }
public interface IExportData { void ExportData(IModel<T> model); }
public class Persons: IModel<Person> {
// ToDo: Implement GetAll().
}
public class ExportExcel: IExportData
{
public void ExportData(IModel<T> model)
{
List<T> entities = model.GetAll();
}
}
public static class ExportPersons
{
public static void PerformExport(IModel<Person> model)
{
ExportExcel exportExcel = new ExportExcel();
exportExcel.ExportData(model);
}
}
private void Form1_Load(object sender, EventArgs e)
{
Persons persons = new Persons();
ExportPersons.PerformExport(persons);
}
ASKER