Example #1
0
    //Set an object's Property or Field by the provided name to a value
    public static bool SetObjectValue(this System.Object obj, string name, System.Object value)
    {
        if (obj.GetField(name) != null)
        {
            return(obj.SetFieldValue(name, value));
        }
        else if (obj.GetProperty(name) != null)
        {
            return(obj.SetPropertyValue(name, value));
        }

        Debug.LogWarning("ReflectionF.SetObjectValue: No field or property named " + name + " exists on instance of " + obj.GetType().ShortName());
        return(false);
    }
Example #2
0
        /// <summary>
        /// 填充model
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="control"></param>
        public static void FillModel(Object entity, WebUI.Control control)
        {
            if (entity == null || control == null)
                return;
            NameValueCollection formData = HttpContext.Current.Request.Form;
            PropertyInfo[] propertyList = entity.GetProperties();
            foreach (PropertyInfo pi in propertyList)
            {
                string ctlId = string.Format(IdFormat, pi.Name);
                WebUI.Control ctl = control.FindControl(ctlId);
                if (ctl == null)
                {
                    #region 处理HMTL标签
                    if (formData[ctlId] != null)
                        entity.SetPropertyValue(pi.Name, formData[ctlId]);
                    #endregion
                    continue;
                }
                Type ctlType = ctl.GetType();

                #region 处理服务器控件
                if (ctlType == typeof(WebUI.WebControls.TextBox))//文本框
                {
                    entity.SetPropertyValue(pi.Name, ((WebUI.WebControls.TextBox)ctl).Text);
                }
                else if (ctlType == typeof(WebUI.WebControls.Image))//图片
                {
                    entity.SetPropertyValue(pi.Name, ((WebUI.WebControls.Image)ctl).ImageUrl);
                }
                else if (ctlType == typeof(WebUI.WebControls.DropDownList))//选择框
                {
                    entity.SetPropertyValue(pi.Name, ((WebUI.WebControls.DropDownList)ctl).SelectedValue);
                }
                else if (ctlType == typeof(WebUI.WebControls.HiddenField))//隐藏域
                {
                    entity.SetPropertyValue(pi.Name, ((WebUI.WebControls.HiddenField)ctl).Value);
                }
                else if (ctlType == typeof(WebUI.WebControls.RadioButton))//单选框
                {
                    WebUI.WebControls.RadioButton rb = (WebUI.WebControls.RadioButton)ctl;

                    if (rb.Checked)
                        entity.SetPropertyValue(pi.Name, rb.Text);
                    else
                        entity.SetPropertyValue(pi.Name, "");
                }
                else if (ctlType == typeof(WebUI.WebControls.CheckBox))//复选框
                {
                    WebUI.WebControls.CheckBox ck = (WebUI.WebControls.CheckBox)ctl;
                    if (ck.Checked)
                        entity.SetPropertyValue(pi.Name, ck.Text);
                    else
                        entity.SetPropertyValue(pi.Name, "");
                }
                else if (ctlType == typeof(WebUI.WebControls.CheckBoxList))//复选框列表
                {
                    WebUI.WebControls.CheckBoxList ck = (WebUI.WebControls.CheckBoxList)ctl;
                    string rs = "";
                    foreach (WebUI.WebControls.ListItem li in ck.Items)
                    {
                        if (li.Selected)
                            rs += "," + li.Value;
                    }
                    if (rs.Length > 1)
                    {
                        rs = rs.Substring(1);
                    }
                    entity.SetPropertyValue(pi.Name, rs);
                }
                else if (ctlType == typeof(WebUI.WebControls.RadioButtonList))//单框列表
                {
                    WebUI.WebControls.RadioButtonList ck = (WebUI.WebControls.RadioButtonList)ctl;
                    entity.SetPropertyValue(pi.Name, ck.SelectedValue);
                }
                else if (ctlType == typeof(WebUI.WebControls.ListBox))//列表框
                {
                    WebUI.WebControls.ListBox ck = (WebUI.WebControls.ListBox)ctl;
                    string rs = "";
                    foreach (WebUI.WebControls.ListItem li in ck.Items)
                    {
                        if (li.Selected)
                            rs += "," + li.Value;
                    }
                    if (rs.Length > 1)
                    {
                        rs = rs.Substring(1);
                    }
                    entity.SetPropertyValue(pi.Name, rs);
                }
                #endregion
                #region 处理不同Html控件
                else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputText))//文本域
                {
                    WebUI.HtmlControls.HtmlInputText ct = (WebUI.HtmlControls.HtmlInputText)ctl;
                    entity.SetPropertyValue(pi.Name, ct.Value);
                }
                else if (ctlType == typeof(WebUI.HtmlControls.HtmlTextArea))//文本域
                {
                    WebUI.HtmlControls.HtmlTextArea ct = (WebUI.HtmlControls.HtmlTextArea)ctl;
                    entity.SetPropertyValue(pi.Name, ct.Value);
                }
                else if (ctlType == typeof(WebUI.HtmlControls.HtmlSelect))//选择域
                {
                    WebUI.HtmlControls.HtmlSelect ct = (WebUI.HtmlControls.HtmlSelect)ctl;
                    entity.SetPropertyValue(pi.Name, ct.Items[ct.SelectedIndex].Value);
                }
                else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputHidden))////隐藏域
                {
                    WebUI.HtmlControls.HtmlInputHidden ct = (WebUI.HtmlControls.HtmlInputHidden)ctl;
                    entity.SetPropertyValue(pi.Name, ct.Value);
                }
                else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputRadioButton))//单选域
                {
                    WebUI.HtmlControls.HtmlInputRadioButton rb = (WebUI.HtmlControls.HtmlInputRadioButton)ctl;
                    if (rb.Checked)
                        entity.SetPropertyValue(pi.Name, rb.Value);
                }
                else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputCheckBox))//复选域
                {
                    WebUI.HtmlControls.HtmlInputCheckBox ck = (WebUI.HtmlControls.HtmlInputCheckBox)ctl;
                    if (ck.Checked)
                        entity.SetPropertyValue(pi.Name, ck.Value);
                }
                else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputPassword))//密码域
                {
                    WebUI.HtmlControls.HtmlInputPassword ck = (WebUI.HtmlControls.HtmlInputPassword)ctl;
                    entity.SetPropertyValue(pi.Name, ck.Value);
                }
                #endregion
            }
        }