繁体   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