Beispiel #1
0
    //Get an object's Property or Field value by a provided name and type.
    public static T GetObjectValue <T>(this System.Object obj, string name)
    {
        if (obj.GetField(name) != null)
        {
            return(obj.GetFieldValue <T>(name));
        }
        else if (obj.GetProperty(name) != null)
        {
            return(obj.GetPropertyValue <T>(name));
        }

        Debug.LogWarning("ReflectionF.GetObjectValue: No field or property named " + name + " exists on instance of " + obj.GetType().ShortName());
        return(default(T));
    }
Beispiel #2
0
 /// <summary>
 /// 填充表单
 /// </summary>
 /// <param name="entity"></param>
 /// <param name="control"></param>
 public static void FillForm(WebUI.Control control, Object entity)
 {
     if (entity == null || control == null)
         return;
     PropertyInfo[] propertyList = entity.GetProperties();
     foreach (PropertyInfo pi in propertyList)
     {
         WebUI.Control ctl = control.FindControl(string.Format(IdFormat, pi.Name));
         if (ctl == null)
             continue;
         Type ctlType = ctl.GetType();
         #region 处理服务器控件
         if (ctlType == typeof(WebUI.WebControls.TextBox))//文本框
         {
             if (entity.GetPropertyValue(pi.Name) != null)
                 ((WebUI.WebControls.TextBox)ctl).Text = entity.GetPropertyValue(pi.Name).ToString();
         }
         else if (ctlType == typeof(WebUI.WebControls.Image))//图片
         {
             if (entity.GetPropertyValue(pi.Name) != null)
             {
                 string imageUrl = entity.GetPropertyValue(pi.Name).ToString();
                 if (!string.IsNullOrEmpty(imageUrl))
                     ((WebUI.WebControls.Image)ctl).ImageUrl = imageUrl;
             }
         }
         else if (ctlType == typeof(WebUI.WebControls.DropDownList))//选择框
         {
             if (entity.GetPropertyValue(pi.Name) != null)
                 ((WebUI.WebControls.DropDownList)ctl).SelectedValue = entity.GetPropertyValue(pi.Name).ToString();
         }
         else if (ctlType == typeof(WebUI.WebControls.HiddenField))//隐藏域
         {
             if (entity.GetPropertyValue(pi.Name) != null)
                 ((WebUI.WebControls.HiddenField)ctl).Value = entity.GetPropertyValue(pi.Name).ToString();
         }
         else if (ctlType == typeof(WebUI.WebControls.RadioButton))//单选框
         {
             WebUI.WebControls.RadioButton rb = (WebUI.WebControls.RadioButton)ctl;
             if (entity.GetPropertyValue(pi.Name) != null)
             {
                 rb.Checked = entity.GetPropertyValue(pi.Name).ToString() == rb.Text ? true : false;
             }
         }
         else if (ctlType == typeof(WebUI.WebControls.CheckBox))//复选框
         {
             WebUI.WebControls.CheckBox ck = (WebUI.WebControls.CheckBox)ctl;
             if (entity.GetPropertyValue(pi.Name) != null)
             {
                 ck.Checked = entity.GetPropertyValue(pi.Name).ToString() == ck.Text ? true : false;
             }
         }
         else if (ctlType == typeof(WebUI.WebControls.CheckBoxList))//复选框列表
         {
             WebUI.WebControls.CheckBoxList ck = (WebUI.WebControls.CheckBoxList)ctl;
             if (entity.GetPropertyValue(pi.Name) != null)
             {
                 string sel = entity.GetPropertyValue(pi.Name).ToString();
                 foreach (WebUI.WebControls.ListItem li in ck.Items)
                 {
                     if (sel.IndexOf(",") > -1 && (sel.IndexOf(li.Value + ",") > -1 || sel.IndexOf("," + li.Value) > -1))
                     {
                         li.Selected = true;
                     }
                     else if (sel.IndexOf(",") == -1 && sel == li.Value)
                     {
                         li.Selected = true;
                     }
                     else
                     {
                         li.Selected = false;
                     }
                 }
             }
         }
         else if (ctlType == typeof(WebUI.WebControls.RadioButtonList))//单框列表
         {
             WebUI.WebControls.RadioButtonList ck = (WebUI.WebControls.RadioButtonList)ctl;
             if (entity.GetPropertyValue(pi.Name) != null)
                 ck.SelectedValue = entity.GetPropertyValue(pi.Name).ToString();
         }
         else if (ctlType == typeof(WebUI.WebControls.ListBox))//列表框
         {
             WebUI.WebControls.ListBox ck = (WebUI.WebControls.ListBox)ctl;
             if (entity.GetPropertyValue(pi.Name) != null)
             {
                 string sel = entity.GetPropertyValue(pi.Name).ToString();
                 foreach (WebUI.WebControls.ListItem li in ck.Items)
                 {
                     if (sel.IndexOf(",") > -1 && (sel.IndexOf(li.Value + ",") > -1 || sel.IndexOf("," + li.Value) > -1))
                     {
                         li.Selected = true;
                     }
                     else if (sel.IndexOf(",") == -1 && sel == li.Value)
                     {
                         li.Selected = true;
                     }
                     else
                     {
                         li.Selected = false;
                     }
                 }
             }
         }
         #endregion
         #region 处理不同Html控件
         else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputText))//文本域
         {
             WebUI.HtmlControls.HtmlInputText ct = (WebUI.HtmlControls.HtmlInputText)ctl;
             if (entity.GetPropertyValue(pi.Name) != null)
                 ct.Value = entity.GetPropertyValue(pi.Name).ToString();
         }
         else if (ctlType == typeof(WebUI.HtmlControls.HtmlTextArea))//文本域
         {
             WebUI.HtmlControls.HtmlTextArea ct = (WebUI.HtmlControls.HtmlTextArea)ctl;
             if (entity.GetPropertyValue(pi.Name) != null)
                 ct.Value = entity.GetPropertyValue(pi.Name).ToString();
         }
         else if (ctlType == typeof(WebUI.HtmlControls.HtmlSelect))//选择域
         {
             WebUI.HtmlControls.HtmlSelect ct = (WebUI.HtmlControls.HtmlSelect)ctl;
             if (entity.GetPropertyValue(pi.Name) != null)
             {
                 for (int i = 0; i < ct.Items.Count; i++)
                 {
                     if (ct.Items[i].Value == entity.GetPropertyValue(pi.Name).ToString())
                         ct.SelectedIndex = i;
                 }
             }
         }
         else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputHidden))////隐藏域
         {
             WebUI.HtmlControls.HtmlInputHidden ct = (WebUI.HtmlControls.HtmlInputHidden)ctl;
             if (entity.GetPropertyValue(pi.Name) != null)
                 ct.Value = entity.GetPropertyValue(pi.Name).ToString();
         }
         else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputRadioButton))//单选域
         {
             WebUI.HtmlControls.HtmlInputRadioButton rb = (WebUI.HtmlControls.HtmlInputRadioButton)ctl;
             if (rb.Checked && entity.GetPropertyValue(pi.Name) != null)
             {
                 rb.Checked = entity.GetPropertyValue(pi.Name).ToString() == rb.Value ? true : false;
             }
         }
         else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputCheckBox))//复选域
         {
             WebUI.HtmlControls.HtmlInputCheckBox ck = (WebUI.HtmlControls.HtmlInputCheckBox)ctl;
             if (entity.GetPropertyValue(pi.Name) != null)
                 if (entity.GetPropertyValue(pi.Name).ToString().IndexOf("," + ck.Value) != -1)
                     ck.Checked = true;
         }
         else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputPassword))//密码域
         {
             WebUI.HtmlControls.HtmlInputPassword ck = (WebUI.HtmlControls.HtmlInputPassword)ctl;
             if (entity.GetPropertyValue(pi.Name) != null)
                 ck.Value = entity.GetPropertyValue(pi.Name).ToString();
         }
         #endregion
     }
 }