简体   繁体   中英

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"

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> :

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM