gpt4 book ai didi

c# - 我该如何修复这个 : generic type in list?

转载 作者:行者123 更新时间:2023-11-30 20:28:35 25 4
gpt4 key购买 nike

我遇到编译错误:

"The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)
Error 3 The type or namespace name 'S' could not be found (are you missing a using directive or an assembly reference?)

下面是我的代码:

public static List<T> AutoCopyList(List<S> lst)
{
List<T> ModelF = new List<T>();
foreach (var item in lst)
{
var e = new T();
e = Commn.AutoCopy<T, S>(item);
ModelF.Add(e);
}
return ModelF;
}

最佳答案

您应该将您的方法标记为通用方法并明确声明 T 应该有一个公共(public)无参数构造函数:

 public static List<T> AutoCopyList<S,T>(List<S> lst) where T : new()

有关新约束的更多信息,where T : new(),请查看 here .

关于c# - 我该如何修复这个 : generic type in list?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47133260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com