我创建了一个查询表的函数,并获取结果并填充对象集合,将列名与属性匹配。
太慢了,我需要优化它。有没有不同的技术来做同样的事情更快?似乎是CallByName是主要的减速。
我很确定我可以通过使用早期绑定并为每种对象类型定制一个函数来加快速度,但这不是理想的结果。
谢谢, 布拉德
Public Function GetRecords(Sql As String, ConnStr As String, ObjType As Object) As List(Of Object)
Dim objList As New List(Of Object)()
Dim SingleObj As Object
Dim ColumnName As String
Dim dt As DataTable = ReturnTableQuery(Sql, ConnStr)
If dt Is Nothing Then Return Nothing
For Each row As DataRow In dt.Rows
SingleObj = Activator.CreateInstance(Type.GetType(ObjType.ToString))
For Each col As DataColumn In dt.Columns
ColumnName = col.ColumnName
If row(ColumnName).ToString <> "" Then
Try
CallByName(SingleObj, ColumnName, CallType.Set, row(ColumnName))
Catch ex As Exception
End Try
End If
Next
objList.Add(SingleObj)
Next
Return objList
End Function