2017-04-12 24 views
0

パラメータとジェネリック型をとるメソッドreadExcelFile()を持っています。ジェネリック型はクラスなので、このメソッドにはこのメソッドを渡す必要があります。 。インターフェイスでIList を定義するにはどうすればいいですか

すべて正常に動作している状態以上が、私はまた、インターフェースに

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'の宣言があることに注目してください。

関連する問題