2016-03-25 48 views
0

對象我有這種情況:創建泛型類的列表

public class ExtResult<T> 
{ 
    public bool Success { get; set; } 
    public string Msg { get; set; } 
    public int Total { get; set; } 
    public T Data { get; set; } 
} 

//create list object: 
List<ProductPreview> gridLines; 
... 
... 
//At the end i would like to create object 
ExtResult<gridLines> result = new ExtResult<gridLines>() { 
    Success = true, Msg = "", 
    Total=0, 
    Data = gridLines 
} 

但我得到一個錯誤:

error: "cannot resolve gridLines"

我能做些什麼來解決這個問題?

+0

「*什麼是正確的方法?*」 - 做什麼? (並且最可能的答案是瞭解泛型) – Amit

回答

4

gridLines是一個變量,其類型爲List<ProductPreview>,你應該爲類型參數ExtResult<T>使用:

ExtResult<List<ProductPreview>> result = new ExtResult<List<ProductPreview>>() { 
    Success = true, 
    Msg = "", 
    Total=0, 
    Data = gridLines 
}; 
+0

當然,謝謝。我被一些例子誤導了:) – Simon

2

你應該傳遞一個類型作爲一般的參數,而不是一個變量:

var result = new ExtResult<List<ProductPreview>> // not gridLines, but it's type 
{ 
    Success = true, 
    Msg = "", 
    Total=0, 
    Data = gridLines 
}