简体   繁体   English

如何定义IList <T> 在界面中

[英]How do I define IList<T> in Interface

I have method readExcelFile() which takes parameter and generic type where generic type is class so the idea is give this method require parameter to read excel file and I get List of object having excel records. 我有readExcelFile()方法,该方法带有参数和泛型类型,而泛型类型是类,因此我们的想法是将此方法要求参数读取excel文件,然后得到具有excel记录的对象列表。

The all is working fine that state above but I also need to define interface 上面的状态一切正常,但我还需要定义接口

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

and in above code I am getting error 在上面的代码中我得到了错误

 The Type or namespace T could not be found(missing directive or assembly )

implementation code 实施代码

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

You need to define a generic interface as well 您还需要定义一个通用接口

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

The compiler doesn't know how to determine the T later. 编译器不知道以后如何确定T

The class then, will be defined like this: 然后,该类的定义如下:

public class ProcessExcel<T>: IProcessExcel<T> where T: class

You need to implement the generic method correctly in both Interface and in Class Like this : 您需要正确地实现通用的方法在两个接口和类就像这样:

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

Notice the declaration of 'T' after the Method Name. 注意方法名称后的“ T”声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM