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

前端 未结 5 749
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 01:09
public void Getrecords(ref IList iList,T dataItem) 
{ 
  iList = Populate.GetList() // GetListis defined as GetList
}

data

相关标签:
5条回答
  • 2020-12-02 01:46
    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>();
    }
    
    0 讨论(0)
  • 2020-12-02 01:50

    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>();
    
    0 讨论(0)
  • 2020-12-02 01:57

    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);
    }
    
    0 讨论(0)
  • 2020-12-02 01:57
    Getrecords<T> ...
    

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

    0 讨论(0)
  • 2020-12-02 02:10

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

    0 讨论(0)
提交回复
热议问题