public void Getrecords(ref IList iList,T dataItem)
{
iList = Populate.GetList<dataItem>() // GetListis defined as GetList<T>
}
dataItem 可以是我的订单对象或用户对象,这将在运行时决定。上面的内容不起作用,因为它给了我这个错误
类型“T”必须具有公共无参数构造函数才能将其用作泛型类型中的参数“T”
public void GetRecords<T>(ref IList<T> iList, T dataitem)
{
}
您还在寻找什么?
修改后的问题:
iList = Populate.GetList<dataItem>()
“数据项”是一个变量。您想在那里指定一个类型:
iList = Populate.GetList<T>()
类型“T”必须具有公共
无参数构造函数,以便
将其用作泛型中的参数“T”
类型 GetList:new()
这就是说,当您定义 Populate.GetList() 时,您是这样声明的:
IList<T> GetList<T>() where T: new()
{...}
这告诉编译器 GetList 只能使用具有公共无参数构造函数的类型。你使用 T 在 GetRecords 中创建 GetList 方法(这里 T 指的是不同的类型),你必须对其进行相同的限制:
public void GetRecords<T>(ref IList<T> iList, T dataitem) where T: new()
{
iList = Populate.GetList<T>();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)