为了在函数中创建泛型类型的实例,您必须使用“ new”标志对其进行约束。
public static string GetAllItems<T>(...) where T : new()
但是,这仅在您要调用没有参数的构造函数时才有效。这里不是这样。相反,您必须提供另一个参数,该参数允许基于参数创建对象。最简单的是功能。
public static string GetAllItems<T>(..., Func<ListItem,T> del) { ... List<T> tabListItems = new List<T>(); foreach (ListItem listItem in listCollection) { tabListItems.Add(del(listItem)); } ...}然后可以这样称呼它
GetAllItems<Foo>(..., l => new Foo(l));



