2016-03-25 49 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 
}