反射应用之一:根据控件名、属性名进行取值和赋值
2004-08-17 00:32
483 查看
'必须引用命名空间System.Reflection,System.ComponentModel
'以下根据控件名和属性名取值
Public Function GetValueControlProperty(ByVal ClassInstance As Object, ByVal ControlName As String, ByVal PropertyName As String) As Object
Dim Result As Object
Dim myType As Type = ClassInstance.GetType
Dim myFieldInfo As FieldInfo = myType.GetField("_" & ControlName, BindingFlags.NonPublic Or _
BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.Instance)
If Not myFieldInfo Is Nothing Then
Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(myType)
Dim myProperty As PropertyDescriptor = properties.Find(PropertyName, False)
If Not myProperty Is Nothing Then
Dim ctr As Object
ctr = myFieldInfo.GetValue(ClassInstance)
Try
Result = myProperty.GetValue(ctr)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End If
Return Result
End Function
'以下根据控件名和属性名赋值
Public Function SetValueControlProperty(ByVal ClassInstance As Object, ByVal ControlName As String, ByVal PropertyName As String, ByVal Value As Object) As Object
Dim Result As Object
Dim myType As Type = ClassInstance.GetType
Dim myFieldInfo As FieldInfo = myType.GetField("_" & ControlName, BindingFlags.NonPublic _
Or BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.Instance) '加"_"这个是特要紧的
If Not myFieldInfo Is Nothing Then
Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(myType)
Dim myProperty As PropertyDescriptor = properties.Find(PropertyName, False) '这里设为True就不用区分大小写了
If Not myProperty Is Nothing Then
Dim ctr As Object
ctr = myFieldInfo.GetValue(ClassInstance) '取得控件实例
Try
myProperty.SetValue(ctr, Value)
Result = ctr
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End If
Return Result
End Function
'调用
'以下实现Label1.Text=TextBox1.Text,Label2.Text=TextBox2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 1 To 2
Me.SetValueControlProperty(Me, "Label" & i.ToString, "Text", GetValueControlProperty(Me, "TextBox" & i.ToString, "Text"))
Next i
End Sub
'以下根据控件名和属性名取值
Public Function GetValueControlProperty(ByVal ClassInstance As Object, ByVal ControlName As String, ByVal PropertyName As String) As Object
Dim Result As Object
Dim myType As Type = ClassInstance.GetType
Dim myFieldInfo As FieldInfo = myType.GetField("_" & ControlName, BindingFlags.NonPublic Or _
BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.Instance)
If Not myFieldInfo Is Nothing Then
Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(myType)
Dim myProperty As PropertyDescriptor = properties.Find(PropertyName, False)
If Not myProperty Is Nothing Then
Dim ctr As Object
ctr = myFieldInfo.GetValue(ClassInstance)
Try
Result = myProperty.GetValue(ctr)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End If
Return Result
End Function
'以下根据控件名和属性名赋值
Public Function SetValueControlProperty(ByVal ClassInstance As Object, ByVal ControlName As String, ByVal PropertyName As String, ByVal Value As Object) As Object
Dim Result As Object
Dim myType As Type = ClassInstance.GetType
Dim myFieldInfo As FieldInfo = myType.GetField("_" & ControlName, BindingFlags.NonPublic _
Or BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.Instance) '加"_"这个是特要紧的
If Not myFieldInfo Is Nothing Then
Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(myType)
Dim myProperty As PropertyDescriptor = properties.Find(PropertyName, False) '这里设为True就不用区分大小写了
If Not myProperty Is Nothing Then
Dim ctr As Object
ctr = myFieldInfo.GetValue(ClassInstance) '取得控件实例
Try
myProperty.SetValue(ctr, Value)
Result = ctr
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End If
Return Result
End Function
'调用
'以下实现Label1.Text=TextBox1.Text,Label2.Text=TextBox2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 1 To 2
Me.SetValueControlProperty(Me, "Label" & i.ToString, "Text", GetValueControlProperty(Me, "TextBox" & i.ToString, "Text"))
Next i
End Sub
相关文章推荐
- 反射应用之一:根据控件名、属性名进行取值和赋值
- 反射应用之一:根据控件名、属性名进行取值和赋值
- 根据控件名、属性名进行取值和赋值
- 反射的应用--根据控件名取得控件,获取值及赋值
- 关于在Java使用反射对属性取值和赋值的性能问题。
- 反射应用实例--根据map中的值创建对应并调用set方法给域赋值
- 反射的应用--根据控件名取得控件
- Java反射:根据方法名动态调用方法,解决商品动态属性取值问题。
- C#中,实体属性与数据库字段的进行反射赋值收藏
- Jquery 对.net服务器控件RadioButtonList进行赋值和取值的操作
- 代码 - 通过反射对象属性进行遍历赋值
- geotrellis使用(十一)实现空间数据库栅格化以及根据属性字段进行赋值
- 用反射来进行实体和控件的赋值
- 反射的应用--根据控件名取得控件
- 写一个方法进行各种属性的更新,而不要每个界面设置改变都改变一个属性。就是点击应用或确定按钮时调用这个更新属性的方法,遍历所有(控制对象属性的)界面控件的状态(值),进行属性修改和刷新。
- 反射的应用--根据控件名取得控件
- Jquery 对.net服务器控件RadioButtonList进行赋值和取值的操作
- 利用反射给对象中的某个属性赋值或取值
- 利用反射对对象属性赋值取值操作
- C#winform中使用控件的Dock属性进行布局