我能不能通过反射机制,得到程序中的控件类型,
比如说我的程序中有两个按钮,name: btn1,btn2.  Text:按钮1,按钮2现在是我想通过反射得到这两个按钮的name   也就是 btn1和btn2,不知能不能得到?如果不能得到的话,那有没有其他方式(遍历资源文件也不知行不)

解决方案 »

  1.   


      /// <summary>
            ///  绑定业务对象到控件
            /// </summary>
            /// <param name="obj">业务对象</param>
            /// <param name="container">控件集合</param>
            public static void BindObjectToControls(object obj, System.Web.UI.Control container)
            {
                if (obj == null) return;
                // Get the properties of the business object
                //
                Type objType = obj.GetType();//得到业务对象的类型
                PropertyInfo[] objPropertiesArray = objType.GetProperties();//得到此类型所有属性
                foreach (PropertyInfo objProperty in objPropertiesArray)
                {
                    Control control = container.FindControl(objProperty.Name);//在控件集合里查找和业务对象名称一样的控件
                    if (control == null) continue;
                    // handle ListControls (DropDownList, CheckBoxList, RadioButtonList)
                    //
                    if (control is ListControl)//如果对象是集合类型控件
                    {
                        ListControl listControl = (ListControl)control;
                        string propertyValue = objProperty.GetValue(obj, null).ToString();//得到实体类属性的值
                        ListItem listItem = listControl.Items.FindByValue(propertyValue);
                        if (listItem != null) listItem.Selected = true;
                    }
                    else
                    {
                        // get the properties of the control
                        //
                        Type controlType = control.GetType();
                        PropertyInfo[] controlPropertiesArray = controlType.GetProperties();
                        // test for common properties
                        //
                        bool success = false;
                        success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Checked", typeof(bool));
                        if (!success)
                            success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "SelectedDate", typeof(DateTime));
                        if (!success)
                            success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Value", typeof(String));
                        if (!success)
                            success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Text", typeof(String));
                    }
                }
            }
            /// <summary>
            /// 查找并设置控件属性
            /// </summary>
            /// <param name="obj">业务对象</param>
            /// <param name="objProperty">业务对象属性</param>
            /// <param name="control">控件集合</param>
            /// <param name="controlPropertiesArray">控件属性集合</param>
            /// <param name="propertyName">属性名称</param>
            /// <param name="type">属性类型</param>
            /// <returns></returns>
            public static bool FindAndSetControlProperty(object obj, PropertyInfo objProperty, System.Web.UI.Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type)
            {
                // iterate through control properties
                //
                foreach (PropertyInfo controlProperty in controlPropertiesArray)
                {
                    // check for matching name and type
                    //
                    if (controlProperty.Name == propertyName && controlProperty.PropertyType == type)
                    {
                        // set the control's property to the business object property value
                        //
                        controlProperty.SetValue(control, Convert.ChangeType(objProperty.GetValue(obj, null), type), null);
                        return true;
                    }
                }
                return false;
            }
            /// <summary>
            /// 绑控件到定业务对象
            /// </summary>
            /// <param name="obj">业务对象</param>
            /// <param name="container">控件集合</param>
            public static void BindControlsToObject(object obj, System.Web.UI.Control container)
            {
                if (obj == null) return;
                // Get the properties of the business object
                //  
                Type objType = obj.GetType();
                PropertyInfo[] objPropertiesArray = objType.GetProperties();
                foreach (PropertyInfo objProperty in objPropertiesArray)
                {
                    Control control = container.FindControl(objProperty.Name);
                    if (control == null) continue;
                    if (control is ListControl)
                    {
                        ListControl listControl = (ListControl)control;
                        if (listControl.SelectedItem != null)
                            objProperty.SetValue(obj, Convert.ChangeType(listControl.SelectedItem.Value, objProperty.PropertyType), null);
                    }
                    else
                    {
                        // get the properties of the control
                        //
                        Type controlType = control.GetType();
                        PropertyInfo[] controlPropertiesArray = controlType.GetProperties();
                        // test for common properties
                        //
                        bool success = false;
                        success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Checked", typeof(bool));
                        if (!success)
                            success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "SelectedDate", typeof(DateTime));
                        if (!success)
                            success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Value", typeof(String));
                        if (!success)
                            success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Text", typeof(String));
                    }
                }
            }
     /// <summary>
            /// 查找并得到控件属性
            /// </summary>
            /// <param name="obj">业务对象</param>
            /// <param name="objProperty">业务对象属性</param>
            /// <param name="control">控件集合</param>
            /// <param name="controlPropertiesArray">控件属性集合</param>
            /// <param name="propertyName">属性名称</param>
            /// <param name="type">属性类型</param>
            /// <returns></returns>
            public static bool FindAndGetControlProperty(object obj, PropertyInfo objProperty, System.Web.UI.Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type)
            {
                // 在整个控件属性中进行迭代
                foreach (PropertyInfo controlProperty in controlPropertiesArray)
                {
                    // 检查匹配的名称和类型
                    if (controlProperty.Name == "Text" && controlProperty.PropertyType == typeof(String))
                    {
                        // 将控件的属性设置为
                        // 业务对象属性值
                        try
                        {
                            objProperty.SetValue(obj, Convert.ChangeType(controlProperty.GetValue(control, null), objProperty.PropertyType), null);
                            return true;
                        }
                        catch
                        {
                            // 无法将来自窗体控件
                            // 的数据转换为
                            // objProperty.PropertyType
                            return false;
                        }
                    }
                }
                return true;
            }
      

  2.   

    耐住性子看看反射,有一个小时你就搞明白这个问题了基础不会问Google或百度先,学的更快
      

  3.   

    为什么要反射呢?foreach (Control ctrl in this.Controls)
    {
       if (ctrl is Button)
          list.Add((ctrl as Button).Name);
    }
      

  4.   

    [Quote=引用 6 楼 acqy 的回复:]
    为什么要反射呢?
    C# codeforeach (Control ctrlinthis.Controls)
    {if (ctrlis Button)
          list.Add((ctrlas Button).Name);
    }
    上面的代码并不是所有的控件都能获得,例如MenuItem,StatusPanel等等
      

  5.   

    随时都可以学习,可以探讨一下
    我现在通过解析程序集,获取里面控件的名字和text属性值,来生成txt或xml文件,实现得差不多了,可是对于TreeView控件中的节点,用反射好像获取不到,还没找到解决方案,
      

  6.   

    Control[] ctrls = Controls.Find("", true);
                if (ctrls.Length > 0)
                {
                    Control ctrl = ctrls[0];
                    PropertyInfo pi = ctrl.GetType().GetProperty("");
                    if (pi != null)
                    {
                        object val = pi.GetValue(ctrl, null);
                        if (val != null)
                            MessageBox.Show(val.ToString());
                    }
                }
      

  7.   

    我用的是vs2003
    Control[] ctrls = Controls.Find("", true);
    Controls没有Find();Controls里不包括TreeView下的节点,菜单项也没有
    怎么办??