2017-04-12 40 views
0

我有方法readExcelFile(),它采用参数和泛型类型,其中泛型类型是类,所以想法是给这个方法require参数来读取excel文件,我得到具有excel记录的对象列表。我如何定义界面中的IList <T>

的所有工作正常,这种状态之上,但我还需要定义接口

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; 
    } 

回答

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 
+0

这意味着它会改变实现类中的实现! – toxic

+0

不,它没有。它会让你在实例化时定义'T'。 –

0

您需要正确地实现通用的方法接口和 类像这样:

public interface IProcessExcel { IList<T> ReadExcelFile<T>(string filePath, int readExcelRowFrom, int headerRow);
}

注意方法名称后的'T'声明。

相关问题