簡體   English   中英

創建具有通用類作為列表的對象

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM