【发布时间】:2015-07-13 19:29:02
【问题描述】:
我有一个 C# 抽象类,我将其用作一种接口
public abstract class ExcelParser
{
protected FileInfo fileInfo { get; set; }
protected bool FileIsValidExcelFile()...
protected virtual string GetCellValue(int row, int column)...
protected virtual int GetColumnLocation(string columnHeader)...
public abstract IEnumerable<T> Parse();
}
我的问题是 T 方法的抽象 IEnumerable,Parse()。
我的问题是我希望该方法返回特定类型的 IEnumerable,但我不在乎该类型在抽象级别是什么。我只希望继承类具体说明返回的 IEnumerable。
忽略无法编译的事实,我遇到的另一个问题是表示层中的执行。
private string BuildSql(string fileName, bool minifySqlText, bool productCodeXref)
{
string result = string.Empty;
ISqlBuilder sqlBuilder;
ExcelParser excelParser;
try
{
if (productCodeXref)
{
excelParser = new ProductCodeXrefExcelParser(fileName);
var productCodeXrefs = excelParser.Parse();
sqlBuilder = new ProductCodeXrefSqlBuilder(productCodeXrefs)
}
else
{
excelParser = new VendorExcelParser(fileName);
var vendors = excelParser.Parse();
sqlBuilder = new VendorSqlBuilder(vendors);
}
result = sqlBuilder.GetSql();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "USER ERROR",
MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
return result;
}
目前这是表示层中的一个粗略实现,但我不知道这是否可行。我提出这个问题的原因是因为我有另一个可以编译的 ExcelParser 实现,但这需要我具体说明 ExcelParser 是什么,例如...
ExcelParser<Vendor>
...这完全违背了这样做的目的。
我知道这一点是因为我尝试了与以下链接类似的解决方案,但是我的类/接口要求我指定类型。 => How do i return IEnumerable<T> from a method
有什么办法 1) 让抽象类或接口中的方法返回 T 的 IEnumerable,但不在乎该类型在实现时是什么类型,并且 2) 确保接口/抽象类不关心 Parse 方法将返回什么类型?
【问题讨论】:
-
public abstract class ExcelParser<T>?