Why does this generic method require T to have a public, parameterless constructor?

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


Solution 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>();
}

Solution 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>();

Solution 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);
}