我有方法readExcelFile(),它采用参数和泛型类型,其中泛型类型是类,所以想法是给这个方法需要参数来读取excel文件,我得到具有excel记录的对象列表。
上面的状态正常,但我还需要定义接口
public interface IProcessExcel
{
IList<T> ReadExcelFile(string filePath, int readExcelRowFrom, int headerRow);
}
并在上面的代码中我收到错误
The Type or namespace T could not be found(missing directive or assembly )
实施代码
public class ProcessExcel<T>: IProcessExcel where T: class
{
private static Excel.Application xlApp;
private static Excel.Workbook xlWorkbook;
private static Excel.Worksheet xlWorkSheet;
private static Excel.Range xlRange;
private string GivenFilePath = string.Empty;
private IList<string> ExcelFileExtensionList = new string[] { ".xls", ".xlsx", ".xlsm", ".xltx", ".xltm" };
private T newObject;
List<T> ExcelRecordList = new List<T>();
public ProcessExcel()
{
}
public IList<T> ReadExcelFile(string filePath, int readExcelRowFrom, int headerRow)
{
this.FilePath = filePath;
this.ReadExcelRowFrom = readExcelRowFrom;
this.HeaderRow = headerRow;
this.ReferenceClass = typeof(T);
this.ValidateExcelFile();
this.ProcessReadExcelFile();
return ReadExcelFile;
}
答案 0 :(得分:2)
您还需要定义通用接口
public interface IProcessExcel<T> where T:class
{
IList<T> ReadExcelFile(string filePath, int readExcelRowFrom, int headerRow);
}
编译器不知道如何稍后确定T
。
然后,这个类将被定义为:
public class ProcessExcel<T>: IProcessExcel<T> where T: class
答案 1 :(得分:0)
您需要在 接口和类中正确实现通用方法 像这样:
public interface IProcessExcel
{
IList<T> ReadExcelFile<T>(string filePath, int readExcelRowFrom, int headerRow);
}
请注意&#39; T&#39;在方法名称之后。