[英]Create object with generic class as list
I have this situation: 我有这种情况:
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
}
But I get an error: 但是我得到一个错误:
error: "cannot resolve gridLines"
错误:“无法解析gridLines”
What can I do to fix this? 我该怎么做才能解决此问题?
gridLines
is a variable, its type is List<ProductPreview>
which you should use as the type parameter to ExtResult<T>
: gridLines
是一个变量,其类型为List<ProductPreview>
,应将其用作ExtResult<T>
的类型参数:
ExtResult<List<ProductPreview>> result = new ExtResult<List<ProductPreview>>() {
Success = true,
Msg = "",
Total=0,
Data = gridLines
};
You should pass a type as a generic argument, not a variable: 您应该将类型作为通用参数而不是变量传递:
var result = new ExtResult<List<ProductPreview>> // not gridLines, but it's type
{
Success = true,
Msg = "",
Total=0,
Data = gridLines
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.