是否可以从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)。
任何提示?这可以用反射来完成吗?
_thanks
更新: 管理得到这个工作
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