using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.IO;
using System.Text;namespace ProjectDemo.Common
{
public static class EntityCopy
{
#region
/// <summary>
/// 通过搜烦面对应控g来构造Model对象(要求控g必须?#8220;_Model的属性名”来命?如:_Name)Qƈ大小写一?
/// </summary>
/// <param name="model">要构造的Model对象()</param>
/// <param name="parentControl">控g的容?比如Page或者Master的站位控?</param>
/// <returns>q回参数里model</returns>
public static T GetModel<T>(T model, Control parentControl)
{
Type t = model.GetType();
PropertyInfo[] pi = t.GetProperties();
foreach (PropertyInfo p in pi)
{
SetControlValueToModel(model, p, parentControl);
}
return model;
}
/// <summary>
/// 把页面控件上的D值给Model对象(要求控g必须?#8220;_Model的属性名”来命?如:_Name)Qƈ大小写一?
/// </summary>
/// <param name="model">要赋值的Model对象</param>
/// <param name="p">某个属?lt;/param>
/// <param name="parentControl">控g的容?比如Page或者Master的站位控?</param>
private static void SetControlValueToModel(object model, PropertyInfo p, Control parentControl)
{
Control control = parentControl.FindControl("_" + p.Name);
if (control != null)
{
Type t_c = control.GetType();
switch (t_c.FullName)
{
case "System.Web.UI.WebControls.TextBox": SetValue(model, p, ((TextBox)control).Text); break;
case "System.Web.UI.WebControls.CheckBox": SetValue(model, p, ((CheckBox)control).Checked); break;
case "System.Web.UI.WebControls.CheckBoxList":
CheckBoxList cbl = ((CheckBoxList)control);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < cbl.Items.Count; i++)
{
if (cbl.Items[i].Selected)
{
sb.Append(i);
sb.Append(",");
}
}
SetValue(model, p, sb.ToString().TrimEnd(',')); break;
case "System.Web.UI.WebControls.Image": SetValue(model, p, ((Image)control).ImageUrl); break;
case "System.Web.UI.WebControls.DropDownList": SetValue(model, p, ((DropDownList)control).SelectedValue); break;
case "System.Web.UI.WebControls.RadioButtonList": SetValue(model, p, ((RadioButtonList)control).SelectedValue); break;
case "System.Web.UI.WebControls.HiddenField": SetValue(model, p, ((HiddenField)control).Value); break;
default: break;
}
}
}
/// <summary>
/// 把Dl指定Model对象指定属性上
/// </summary>
/// <param name="model">需要赋值的Model对象</param>
/// <param name="p">某个属?lt;/param>
/// <param name="value">要赋lModel的属性的?lt;/param>
private static void SetValue(object model, PropertyInfo p, object value)
{
if (p.PropertyType.FullName == "System.Guid")
{
p.SetValue(model, new Guid(value.ToString()), null);
}
else
{
p.SetValue(model, Convert.ChangeType(value, p.PropertyType), null);
}
}
#endregion
#region 反射Modell定面控g
/// <summary>
/// l定Model的值到面上对应控?要求控g必须?#8220;_Model的属性名”来命?如:_Name)Qƈ大小写一?
/// </summary>
/// <param name="model">赋好值的Model</param>
/// <param name="parentControl">控g的容?比如Page或者Master的站位控?</param>
public static void BindControls(object model, Control parentControl)
{
Type t = model.GetType();
PropertyInfo[] pi = t.GetProperties();
foreach (PropertyInfo p in pi)
{
SetModelValueToControl(model, p, parentControl);
}
}
/// <summary>
/// 把Model的Dl页面上的控?目前只针对Web)
/// </summary>
/// <param name="model">赋好值的Model</param>
/// <param name="p">Model的某个属?lt;/param>
/// <param name="parentControl">控g的容?比如Page或者Master的站位控?</param>
private static void SetModelValueToControl(object model, PropertyInfo p, Control parentControl)
{
Control control = parentControl.FindControl("_" + p.Name);
if (control != null)
{
Type t_c = control.GetType();
switch (t_c.FullName)
{
case "System.Web.UI.WebControls.TextBox": ((TextBox)control).Text = p.GetValue(model, null).ToString(); break;
case "System.Web.UI.WebControls.Label": ((Label)control).Text = p.GetValue(model, null).ToString(); break;
case "System.Web.UI.WebControls.Literal": ((Literal)control).Text = p.GetValue(model, null).ToString(); break;
case "System.Web.UI.WebControls.Image": ((Image)control).ImageUrl = p.GetValue(model, null).ToString(); break;
case "System.Web.UI.WebControls.DropDownList": ((DropDownList)control).SelectedValue = p.GetValue(model, null).ToString(); break;
case "System.Web.UI.WebControls.RadioButtonList": ((RadioButtonList)control).SelectedValue = p.GetValue(model, null).ToString(); break;
case "System.Web.UI.WebControls.CheckBox": ((CheckBox)control).Checked = (bool)p.GetValue(model, null); break;
case "System.Web.UI.WebControls.CheckBoxList":
string[] arr = ((string)p.GetValue(model, null)).Split(',');
CheckBoxList cbl = ((CheckBoxList)control);
for (int i = 0; i < arr.Length; i++)
{
cbl.Items[int.Parse(arr[i])].Selected = true;
}
break;
case "System.Web.UI.WebControls.HiddenField": ((HiddenField)control).Value = p.GetValue(model, null).ToString(); break;
default: break;
}
}
}
#endregion
}
public class Person
{
public string Name { set; get; }
public string Sex { set; get; }
}
}

]]>