请看下面的代码:
Public MustInherit Class clsType
'Public Overridable Sub PopulateDataTable(ByVal columns As DataColumnCollection, ByRef objType As clsType, ByVal row As DataRow)
Public Overridable Sub PopulateDataTable(ByVal columns As DataColumnCollection, ByVal row As DataRow)
For Each column As DataColumn In columns
Dim ColumnName As String = column.ColumnName
Dim type As Type = Me.GetType
Dim properties As PropertyInfo() = type.GetProperties()
For Each PropertyInfo In properties
If PropertyInfo.Name = ColumnName Then
'Dim ColumnValue As String = row(ColumnName)
Dim PropertyInfo2 As PropertyInfo = Me.GetType().GetProperty(PropertyInfo.Name)
'PropertyInfo2.SetValue(Me, Convert.ChangeType(ColumnValue, PropertyInfo2.PropertyType), Nothing)
PropertyInfo2.SetValue(Me, Convert.ChangeType(row(ColumnName), PropertyInfo2.PropertyType), Nothing)
Exit For
End If
Next
Next
End Sub
Public Overridable Sub PopulateDataReader(ByVal objDR As DbDataReader)
objDR.Read()
For value As Integer = 0 To objDR.FieldCount - 1
Dim ColumnName As String = objDR.GetName(value)
Dim type As Type = Me.GetType
Dim properties As PropertyInfo() = type.GetProperties()
For Each PropertyInfo In properties
If PropertyInfo.Name = ColumnName Then
'Ship ship = new Ship();
'Dim ColumnValue As String = objDR.GetValue(value)
If IsDBNull(objDR.GetValue(value)) = False Then
Dim PropertyInfo2 As PropertyInfo = Me.GetType().GetProperty(PropertyInfo.Name)
PropertyInfo2.SetValue(Me, Convert.ChangeType(objDR.GetValue(value), PropertyInfo2.PropertyType), Nothing)
End If
Exit For
End If
Next
Next
End Sub
End Class
Public class TestType
Inherits clsType
Private _TestAttribute1 As Integer
Private _TestAttribute2 As Integer
Public Property TestAttribute1() As Integer
Get
Return _TestAttribute1
End Get
Set(ByVal value As Integer)
_TestAttribute1 = value
End Set
End Property
Public Property TestAttribute2() As Integer
Get
Return _TestAttribute2
End Get
Set(ByVal value As Integer)
_TestAttribute2 = value
End Set
End Property
End Class
我可以在使用数据传输对象的代码中执行类似的操作:
dim objTestType As new clsTestType
objTestType.PopulateDataReader(objDR)
objDR 是一个 .NET 数据阅读器。数据库中的列与类中的属性名称匹配。一旦上面的语句运行,对象 (objTestType) 就会被填充值。
我以前从未见过这样做过。这在架构(设计)和性能方面是不好的做法吗?即使用反射从DataReader/DataTable 填充对象?