Why does this generic method require T to have a p

2019-01-08 01:30发布

问题:

public void Getrecords(ref IList iList,T dataItem) 
{ 
  iList = Populate.GetList<dataItem>() // GetListis defined as GetList<T>
}

dataItem can be my order object or user object which will be decided at run time.The above does not work as it gives me this error The type 'T' must have a public parameterless constructor in order to use it as parameter 'T' in the generic type

回答1:

public void GetRecords<T>(ref IList<T> iList, T dataitem)
{
}

What more are you looking for?

To Revised question:

 iList = Populate.GetList<dataItem>() 

"dataitem" is a variable. You want to specify a type there:

 iList = Populate.GetList<T>() 

The type 'T' must have a public parameterless constructor in order to use it as parameter 'T' in the generic type GetList:new()

This is saying that when you defined Populate.GetList(), you declared it like this:

IList<T> GetList<T>() where T: new() 
{...}

That tells the compiler that GetList can only use types that have a public parameterless constructor. You use T to create a GetList method in GetRecords (T refers to different types here), you have to put the same limitation on it:

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


回答2:

Your revised question passes in dataItem as an object of type T and then tries to use it as a type argument to GetList(). Perhaps you pass dataItem in only as a way to specify T?

If so, the you may want something like so:

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

Then you call that like so:

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


回答3:

The issue with demanding a public, parameterless constructor can only be because Populate.GetList demands it - i.e. has the "T : new()" constraint. To fix this, simply add the same constraint to your method.

Actually, I doubt that ref is a good strategy here. At a push, out might do (since you don't read the value), but a far simpler (and more expected) option is a return value:

public IList<T> GetRecords<T>(T dataItem) where T : new()
{  // MG: what does dataItem do here???
  return Populate.GetList<T>();
}

Of course, at that point, the caller might as well just call Populate.GetList directly!

I suspect you can remove dataItem too... but it isn't entirely clear from the question.

If you don't intend it to be generic (and dataItem is the template object), then you can do this via MakeGenericMethod:

public static IList GetRecords(object dataItem) 
{
    Type type = dataItem.GetType();
    return (IList) typeof(Populate).GetMethod("GetList")
        .MakeGenericMethod(type).Invoke(null,null);
}


回答4:

You can use Generic with < T > that will accept the type in runtime like you want.



回答5:

Getrecords<T> ...

This should have any more detailed information that you need. http://msdn.microsoft.com/en-us/library/twcad0zb(VS.80).aspx



标签: c# generics