VB.NET 获取窗体指定类型控件

公司需要开发一个内部用的产品服务系统,我在想权限管理的功能模块列表怎么方便获取,要求能随程序的修改而自行变化.然后在网上看到了通过反射获取类成员的方式,可以用于这种情况.

#Region "获取窗体所有控件"
        '获取所有成员
        Dim tmpControl As New MDIParentMain
        Dim tmpType As Type = tmpControl.GetType
        Dim tmpFields As Reflection.FieldInfo() = tmpType.GetFields(System.Reflection.BindingFlags.NonPublic Or
                                                                    System.Reflection.BindingFlags.Instance)

        Debug.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
        For Each i001 As Reflection.FieldInfo In tmpFields
            If i001.FieldType Is GetType(Button) OrElse
                i001.FieldType Is GetType(RadioButton) Then

                '获取对应control name的text
                Dim tmpObj As Object = tmpType.GetField(i001.Name,
                                                        System.Reflection.BindingFlags.NonPublic Or
                                                        System.Reflection.BindingFlags.Instance Or
                                                        System.Reflection.BindingFlags.IgnoreCase).
                                                        GetValue(tmpControl)
                Debug.WriteLine($"{i001.Name}:[{CType(tmpObj, Control).Text}]")

            End If
        Next
        Debug.WriteLine("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
#End Region

运行结果:

VB.NET 获取窗体指定类型控件_第1张图片

你可能感兴趣的:(.NET)