using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Reflection;
using System.Web.UI.WebControls;
using System.Data;
namespace szjMapping
{
sealed class MappingPage
{
/// <summary>
/// 綁定業務對象到控件
/// </summary>
/// <param name="obj">業務對象</param>
/// <param name="container">控件集合</param>
public static void BindObjectToControls(object obj, System.Web.UI.Control container)
{
if (obj == null) return;
// Get the properties of the business object
//
Type objType = obj.GetType();//得到業務對象的類型
PropertyInfo[] objPropertiesArray = objType.GetProperties();//得到此類型所有屬性
foreach (PropertyInfo objProperty in objPropertiesArray)
{
Control control = container.FindControl(objProperty.Name);//在控件集合裏查找和業務對象名稱一樣的控件
if (control == null) continue;
// handle ListControls (DropDownList, CheckBoxList, RadioButtonList)
//
if (control is ListControl)//如果對象是集合類型控件
{
ListControl listControl = (ListControl)control;
string propertyValue = objProperty.GetValue(obj, null).ToString();//得到實體類屬性的值
ListItem listItem = listControl.Items.FindByValue(propertyValue);
if (listItem != null) listItem.Selected = true;
}
else
{
// get the properties of the control
//
Type controlType = control.GetType();
PropertyInfo[] controlPropertiesArray = controlType.GetProperties();
// test for common properties
//
bool success = false;
success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Checked", typeof(bool));
if (!success)
success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "SelectedDate", typeof(DateTime));
if (!success)
success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Value", typeof(String));
if (!success)
success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Text", typeof(String));
}
}
}
/// <summary>
/// 查找並設置控件屬性
/// </summary>
/// <param name="obj">業務對象</param>
/// <param name="objProperty">業務對象屬性</param>
/// <param name="control">控件集合</param>
/// <param name="controlPropertiesArray">控件屬性集合</param>
/// <param name="propertyName">屬性名稱</param>
/// <param name="type">屬性類型</param>
/// <returns></returns>
public static bool FindAndSetControlProperty(object obj, PropertyInfo objProperty, System.Web.UI.Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type)
{
// iterate through control properties
//
foreach (PropertyInfo controlProperty in controlPropertiesArray)
{
// check for matching name and type
//
if (controlProperty.Name == propertyName && controlProperty.PropertyType == type)
{
// set the control's property to the business object property value
//
controlProperty.SetValue(control, Convert.ChangeType(objProperty.GetValue(obj, null), type), null);
return true;
}
}
return false;
}
/// <summary>
/// 綁控件到定業務對象
/// </summary>
/// <param name="obj">業務對象</param>
/// <param name="container">控件集合</param>
public static void BindControlsToObject(object obj, System.Web.UI.Control container)
{
if (obj == null) return;
// Get the properties of the business object
//
Type objType = obj.GetType();
PropertyInfo[] objPropertiesArray = objType.GetProperties();
foreach (PropertyInfo objProperty in objPropertiesArray)
{
Control control = container.FindControl(objProperty.Name);
if (control == null) continue;
if (control is ListControl)
{
ListControl listControl = (ListControl)control;
if (listControl.SelectedItem != null)
objProperty.SetValue(obj, Convert.ChangeType(listControl.SelectedItem.Value, objProperty.PropertyType), null);
}
else
{
// get the properties of the control
//
Type controlType = control.GetType();
PropertyInfo[] controlPropertiesArray = controlType.GetProperties();
// test for common properties
//
bool success = false;
success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Checked", typeof(bool));
if (!success)
success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "SelectedDate", typeof(DateTime));
if (!success)
success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Value", typeof(String));
if (!success)
success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Text", typeof(String));
}
}
}
/// <summary>
/// 查找並得到控件屬性
/// </summary>
/// <param name="obj">業務對象</param>
/// <param name="objProperty">業務對象屬性</param>
/// <param name="control">控件集合</param>
/// <param name="controlPropertiesArray">控件屬性集合</param>
/// <param name="propertyName">屬性名稱</param>
/// <param name="type">屬性類型</param>
/// <returns></returns>
public static bool FindAndGetControlProperty(object obj, PropertyInfo objProperty, System.Web.UI.Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type)
{
// 在整個控件屬性中進行迭代
foreach (PropertyInfo controlProperty in controlPropertiesArray)
{
// 檢查匹配的名稱和類型
if (controlProperty.Name == "Text" && controlProperty.PropertyType == typeof(String))
{
// 將控件的屬性設置爲
// 業務對象屬性值
try
{
objProperty.SetValue(obj, Convert.ChangeType(controlProperty.GetValue(control, null), objProperty.PropertyType), null);
return true;
}
catch
{
// 無法將來自窗體控件
// 的數據轉換爲
// objProperty.PropertyType
return false;
}
}
}
return true;
}
/// <summary>
/// 綁定DataRow到業務對象
/// </summary>
/// <param name="obj">業務對象</param>
/// <param name="dr">DataRow</param>
public static void BindDataTableToOjbect(object obj, DataRow dr)
{
if (obj == null) return;
Type objType = obj.GetType();
PropertyInfo[] objpropertyInfoArray = objType.GetProperties();
foreach (PropertyInfo objPropertyInfo in objpropertyInfoArray)
{
object col = dr[objPropertyInfo.Name];
if (col == null) continue;
else
{
try
{
objPropertyInfo.SetValue(obj, Convert.ChangeType(col, objPropertyInfo.PropertyType), null);
}
catch
{
throw;
}
}
}
}
}
}