1

是否可以从 IList 以通用方式创建可排序列表?

我有

Private Class DTO
        Public Property text1 As Integer
        Public Property text2 As String
        Public Property text3 As String
        Public Property text4 As DateTime
    End Class

    Private Function getList() As List(Of DTO)
        Dim l As New List(Of DTO)
        l.Add(New DTO With {.text1 = 1, .text2 = Nothing})
        l.Add(New DTO With {.text1 = 2, .text2 = "xxx"})
        l.Add(New DTO With {.text1 = 3, .text2 = "yyy"})

        For i = 1 To 2000
            l.Add(New DTO With {.text1 = i, .text2 = "a_" & (2000 - i).ToString, .text3 = "test" & i, .text4 = DateTime.Now})
        Next
        Return l
    End Function

 Private Sub fillListSearch(Of T)(lst As SortableBindingList(Of T))
        fillWithList(lst)
    End Sub

    Private Sub fillWithList(ByRef pr_list As IList)
        '--> create a SortableBindingList of this list
        grd.DataSource = pr_list
    End Sub

我需要将功能从 fillListSearch 移到 fillWithList 螺母中,我不知道如何将该 IList 转换为 SortableBindingList(Of T)。

有什么提示吗?这可以通过反射来完成吗?

_谢谢

更新:设法让这个工作

Private Sub fillWithList(ByRef pr_list As IList)


     Dim tList As System.Type = pr_list.GetType()
            Dim typeIntern = tList.GetGenericArguments()(0)
            Dim typeSortList As Type = GetType(SortableBindingList(Of ))
            Dim typeArgs() As Type = {typeIntern}
            Dim constructed As Type = typeSortList.MakeGenericType(typeArgs)
            Dim sortedList As Object = Activator.CreateInstance(constructed, pr_list) 
            grd.DataSource = sortedList 
4

0 回答 0