本帖最后由 lll029 于 2013-07-22 23:11:13 编辑

解决方案 »

  1.   

    通过名字太不灵活了

    if (ComponentTypeName == "Label") PropertyDescriptorName = "Text";            else if (ComponentTypeName == "Textbox") PropertyDescriptorName = "Text";
    这种判断是否ITextControl
    var txtCtrl = ClassInstance as ITextControl;
    if(txtCtrol != null) txtCtrol.Text = Value;
    连反射都不需要了
    而DropDownList或者RadionButtonList判断是否是ListControl
    然后通过SelectedValue或者Index赋值就行了,否则别人继承的DropDownList用你的就不行了,违背了OO的替换原则
    不过这样仍然不够优雅,一般这种问题最好是通过改善设计去解决
      

  2.   

    感谢dongxinxi的回答,参考了其他坛友的帖子,已将函数改造,不过由于控件类型不全,只列举了实际使用中用到的控件的“反射”
    /// <summary>
        /// 根据控件名和属性名赋值
        /// </summary>
        /// <param name="ClassInstance">控件所在实例</param>
        /// <param name="ControlName">控件名</param>
        /// <param name="Value">属性值</param>
        /// <returns></returns>
        public static Object SetValueControlProperty(Object ClassInstance, string ControlName, Object Value)
        {
            Object ctrl;
            Object Result = null;
            Type myType = ClassInstance.GetType();
            FieldInfo myFieldInfo = myType.GetField(ControlName, BindingFlags.NonPublic | BindingFlags.Instance);
            string ComponentTypeName = myFieldInfo.FieldType.Name;
            ctrl = myFieldInfo.GetValue(ClassInstance); //取得控件实例
            if (myFieldInfo != null && (ComponentTypeName == "DropDownList" || ComponentTypeName == "RadioButtonList"))
            {
                var listCtrl = ctrl as ListControl;
                if (listCtrl != null)
                {
                    bool selected = false; 
                    bool finded = false;
                    foreach (ListItem it in ((ListControl)listCtrl).Items)                
                    {                    if (Value.ToString().Trim().ToLower() == it.Value.ToLower().Trim())                        
                        {                            
                            finded = true;                            
                            it.Selected = true;                            
                            break;                        
                        }                        
                        else                        
                        {                            
                            it.Selected = false;                        
                        }                    
                        
                        DropDownList ddl = listCtrl as DropDownList;
                        RadioButtonList rbl = listCtrl as RadioButtonList;                    
                        if ((ddl != null || rbl != null) && selected && it.Selected)//如果是DropDownList则不能多选                    
                        {                        
                            it.Selected = false;                    
                        }                    
                        if (finded)   selected = true;                
                    }
                }
            }        else if (myFieldInfo != null)
            {
                var txtCtrl = ctrl as ITextControl;
                if (txtCtrl != null) txtCtrl.Text = Value.ToString();
                Result = txtCtrl;
                
            }
            return Result;
        }