Reputation: 912
I have a factory which has some concrete sub classes for generating reports (.txt, .csv,.xls) I want to make the interface of the concrete classes to be generic so that i can pass in diff types of parameters (Instead of DataTable i need to use DataSet or some other class instance as argument). Here is my interface.
interface IReportCreator
{
bool Create(DataTable dt);
}
I made the interface as generic..like the one below
interface IReportCreator<T>
{
bool Create(T args);
}
Now my question is how can i return the generic interface from the Factory My previous factory code
class Factory
{
static IReportCreator GetReportCreator(string type)
{
IReportCreator reportCreator = null;
if(type == "txt")
reportCreator = new TextCreator();
if(type == "csv")
reportCreator = new CSVCreator();
}
}
And in the cient.. i call like this IReportCreator repCreator = Factory.GetReportCreator("txt"); repCreator.Create(// the arg); // Here the argument i need to make it as generic class Factory { // i dont know how to return the interface here.. }
Any help will be appreciated greatly..
Upvotes: 3
Views: 390
Reputation: 266
interface IReportCreator<T>
{
bool Create(T t);
}
class Factory
{
public IReportCreator<T> Create<T>();
}
var factory = new Factory();
var reportCreator = factory.Create<DataTable>();
Upvotes: 3