.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
反射应用之一:根据控件名、属性名进行取值和赋值

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

    '必须引用命名空间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




相关文章

相关软件