由于项目中输入文本框居多,所以赋值有时很不方便,为了把实体赋值给相应的文本框,做了一个方法:
Public Shared Sub BatchSetData(ByRef ctl As Control, ByRef entity As Object, ByVal prefixControl As String)
Dim ctlName As String = ""
'若界面上是TableLayoutPanel
If TypeOf (ctl) Is TableLayoutPanel Then
Dim objTableLayout As TableLayoutPanel = CType(ctl, TableLayoutPanel)
Dim objType As Type = entity.GetType()
Dim proInfo() As Reflection.PropertyInfo
proInfo = objType.GetProperties()
Dim _label As Label
'遍历界面上Label的控件
For i As Integer = 0 To proInfo.Length - 1
ctlName = proInfo(i).Name.ToString() + prefixControl
_label = CType(objTableLayout.Controls.Item(ctlName), Label)
'根据实体中不同的类型完成赋值
If _label IsNot Nothing Then
_label.Text = proInfo(i).GetValue(entity, Nothing)
End If
Next
End If
End Sub