我有一个工厂,它有一些用于生成报告的具体子类(.txt,.csv,.xls) 我想使具体类的接口是通用的,这样我就可以传入diff类型的参数(而不是DataTable,我需要使用DataSet或其他类实例作为参数)。这是我的界面。
interface IReportCreator
{
bool Create(DataTable dt);
}
我将界面设为通用..就像下面那样
interface IReportCreator<T>
{
bool Create(T args);
}
现在我的问题是如何从Factory返回通用接口 我以前的工厂代码
class Factory
{
static IReportCreator GetReportCreator(string type)
{
IReportCreator reportCreator = null;
if(type == "txt")
reportCreator = new TextCreator();
if(type == "csv")
reportCreator = new CSVCreator();
}
}
而在这个客户......我这样打电话 IReportCreator repCreator = Factory.GetReportCreator(“txt”); repCreator.Create(// arg); //这里我需要将其作为通用参数 班级工厂 { //我不知道如何在这里返回界面.. }
任何帮助都将受到极大的赞赏..
答案 0 :(得分:3)
interface IReportCreator<T>
{
bool Create(T t);
}
class Factory
{
public IReportCreator<T> Create<T>();
}
var factory = new Factory();
var reportCreator = factory.Create<DataTable>();