將參數(shù)傳遞給模板化類型的C#generic new()我正在嘗試在添加到列表時(shí)通過其構(gòu)造函數(shù)創(chuàng)建類型為T的新對(duì)象。我收到編譯錯(cuò)誤:錯(cuò)誤消息是:'T':創(chuàng)建變量實(shí)例時(shí)無法提供參數(shù)但我的類確實(shí)有一個(gè)構(gòu)造函數(shù)參數(shù)!我怎樣才能做到這一點(diǎn)?public static string GetAllItems<T>(...) where T : new(){
...
List<T> tabListItems = new List<T>();
foreach (ListItem listItem in listCollection)
{
tabListItems.Add(new T(listItem)); // error here.
}
...}
- 3 回答
- 0 關(guān)注
- 618 瀏覽
添加回答
舉報(bào)
0/150
提交
取消