[英]Create object with generic class as list
我有這種情況:
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
}
但是我得到一個錯誤:
錯誤:“無法解析gridLines”
我該怎么做才能解決此問題?
gridLines
是一個變量,其類型為List<ProductPreview>
,應將其用作ExtResult<T>
的類型參數:
ExtResult<List<ProductPreview>> result = new ExtResult<List<ProductPreview>>() {
Success = true,
Msg = "",
Total=0,
Data = gridLines
};
您應該將類型作為通用參數而不是變量傳遞:
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.