Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么这个泛型方法要求T有一个公共的、无参数的构造函数? public void Getrecords(ref-IList-IList,T-dataItem) { iList=Populate.GetList()//GetList定义为GetList }_C#_Generics - Fatal编程技术网

C# 为什么这个泛型方法要求T有一个公共的、无参数的构造函数? public void Getrecords(ref-IList-IList,T-dataItem) { iList=Populate.GetList()//GetList定义为GetList }

C# 为什么这个泛型方法要求T有一个公共的、无参数的构造函数? public void Getrecords(ref-IList-IList,T-dataItem) { iList=Populate.GetList()//GetList定义为GetList },c#,generics,C#,Generics,dataItem可以是我的订单对象或用户对象,这将在运行时决定。上面的操作不起作用,因为它给了我这个错误 类型“T”必须具有公共无参数构造函数,才能将其用作泛型类型中的参数“T”您可以使用泛型with,它将在运行时接受您想要的类型。您可以使用泛型with,它将在运行时接受您想要的类型。Getrecords。。。 public void Getrecords(ref IList iList,T dataItem) { iList = Populate.GetList<dataItem

dataItem可以是我的订单对象或用户对象,这将在运行时决定。上面的操作不起作用,因为它给了我这个错误
类型“T”必须具有公共无参数构造函数,才能将其用作泛型类型中的参数“T”

您可以使用泛型with,它将在运行时接受您想要的类型。

您可以使用泛型with,它将在运行时接受您想要的类型。

Getrecords。。。
public void Getrecords(ref IList iList,T dataItem) 
{ 
  iList = Populate.GetList<dataItem>() // GetListis defined as GetList<T>
}
这应该有您需要的任何更详细的信息。

获取记录。。。
这应该有您需要的任何更详细的信息。

公共作废GetRecords(参考IList IList,T dataitem)
{
}
你还想要什么

修改后的问题:

public void GetRecords<T>(ref IList<T> iList, T dataitem)
{
}
iList=Populate.GetList()
“dataitem”是一个变量。要在此处指定类型:

 iList = Populate.GetList<dataItem>() 
iList=Populate.GetList()
类型“T”必须具有公共 无参数构造函数,以便 将其用作泛型中的参数“T” 键入GetList:new()

这意味着在定义Populate.GetList()时,您将其声明为:

 iList = Populate.GetList<T>() 
IList GetList(),其中T:new()
{...}
这告诉编译器GetList只能使用具有公共无参数构造函数的类型。使用T在GetRecords中创建GetList方法(T在这里表示不同的类型),必须对其进行相同的限制:

IList<T> GetList<T>() where T: new() 
{...}
public void GetRecords(ref-IList-IList,T-dataitem),其中T:new()
{
iList=Populate.GetList();
}
公共作废GetRecords(参考IList IList,T dataitem)
{
}
你还想要什么

修改后的问题:

public void GetRecords<T>(ref IList<T> iList, T dataitem)
{
}
iList=Populate.GetList()
“dataitem”是一个变量。要在此处指定类型:

 iList = Populate.GetList<dataItem>() 
iList=Populate.GetList()
类型“T”必须具有公共 无参数构造函数,以便 将其用作泛型中的参数“T” 键入GetList:new()

这意味着在定义Populate.GetList()时,您将其声明为:

 iList = Populate.GetList<T>() 
IList GetList(),其中T:new()
{...}
这告诉编译器GetList只能使用具有公共无参数构造函数的类型。使用T在GetRecords中创建GetList方法(T在这里表示不同的类型),必须对其进行相同的限制:

IList<T> GetList<T>() where T: new() 
{...}
public void GetRecords(ref-IList-IList,T-dataitem),其中T:new()
{
iList=Populate.GetList();
}

修改后的问题作为类型为T的对象传入dataItem,然后尝试将其用作GetList()的类型参数。也许传递dataItem只是为了指定T

如果是这样的话,您可能希望这样:

public void GetRecords<T>(ref IList<T> iList, T dataitem) where T: new() 
{
   iList = Populate.GetList<T>();
}
public IList GetRecords(){
返回Populate.GetList();
}
那么你这样称呼它:

public IList<T> GetRecords<T>() {
  return Populate.GetList<T>();
}
IList result=GetRecords();

修改后的问题作为类型为T的对象传入dataItem,然后尝试将其用作GetList()的类型参数。也许传递dataItem只是为了指定T

如果是这样的话,您可能希望这样:

public void GetRecords<T>(ref IList<T> iList, T dataitem) where T: new() 
{
   iList = Populate.GetList<T>();
}
public IList GetRecords(){
返回Populate.GetList();
}
那么你这样称呼它:

public IList<T> GetRecords<T>() {
  return Populate.GetList<T>();
}
IList result=GetRecords();

要求使用公共的无参数构造函数的问题只能是因为Populate.GetList需要它,即具有“T:new()”约束。要解决这个问题,只需将相同的约束添加到方法中

实际上,我怀疑
ref
在这里是一个好策略。推送时,
out
可能会这样做(因为您不读取该值),但返回值是一个简单得多的选项(也是更期望的选项):

IList<int> result = GetRecords<int>();

要求使用公共的无参数构造函数的问题只能是因为Populate.GetList需要它,即具有“T:new()”约束。要解决这个问题,只需将相同的约束添加到方法中

实际上,我怀疑
ref
在这里是一个好策略。推送时,
out
可能会这样做(因为您不读取该值),但返回值是一个简单得多的选项(也是更期望的选项):

IList<int> result = GetRecords<int>();

你的问题很模糊,很难理解。。。你想做什么?为什么这个家伙仅仅问了一个问题就被否决了?你能澄清一下dataItem在这里做什么吗?它不能用作泛型类型参数(t can)。那么它有什么作用呢?答案是(在我的帖子上)-我发布的两个场景中的任何一个都有帮助吗?你的问题非常模糊,很难理解。。。你想做什么?为什么这个家伙仅仅问了一个问题就被否决了?你能澄清一下dataItem在这里做什么吗?它不能用作泛型类型参数(t can)。那么它做什么呢?答案是(在我的帖子上)-我发布的两个场景中的任何一个都有帮助吗?我尝试了,我得到了这个结果类型“T”必须有一个公共的无参数构造函数,才能在泛型类型GetList:new()中将其用作参数“T”。如果需要创建一个新的,请指定T:objectOr,您可以使用默认值(T)也要创建一个新实例。其中T:new()是编译器想要的。有一段时间我认为这也是他想要的,然后我意识到他可能已经有了某种类型的对象,他想用它来确定方法的类型参数。即便如此,这仍然可能是正确的方向。只需使用MyDataItem.GetType()作为类型参数。我尝试过,得到的结果是,类型“T”必须有一个公共的无参数构造函数,才能在泛型类型GetList:new()中将其用作参数“T”。如果需要创建一个新实例,请指定T:objectOr,也可以使用默认值(T)创建一个新实例。其中T:new()是编译器想要的我以为这是w