本文整理汇总了C#中System.Object.GetProperties方法的典型用法代码示例。如果您正苦于以下问题:C# Object.GetProperties方法的具体用法?C# Object.GetProperties怎么用?C# Object.GetProperties使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Object
的用法示例。
在下文中一共展示了Object.GetProperties方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FillModel
/// <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);
//.........这里部分代码省略.........
示例2: FillForm
/// <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)
{
//.........这里部分代码省略.........