hirisw

博客园 首页 联系 订阅 管理

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.Text;
using TCHRS.Server.Business.UserCode;
using CommonCore.EntityCore;

namespace Web.SendArchive
{
public static class HMIHelper
{
public static Dictionary<Type, Func<Control>> DicTypeControl ;

public static Dictionary<string, Type> DicStringType;

private static string environment = string.Empty;
public static string Environment {
get {
if (string.IsNullOrEmpty(environment))
environment = System.Web.Configuration.WebConfigurationManager.AppSettings["Environment"].ToString();
return environment; }
}

public static bool IsDemo { get { return Environment == "Demo"; } }

static HMIHelper()
{
DicTypeControl = new Dictionary<Type, Func<Control>>();
DicTypeControl.Add(typeof(string),()=>new TextBox());
DicTypeControl.Add(typeof(Enum),()=>new DropDownList());
DicStringType = new Dictionary<string,Type>();
DicStringType = GetPropertyList();
}


/// <summary>
/// 将控件中的子控件值绑定到业务对象 要求子控件的名字和对象属性的名字一致
/// </summary>
/// <param name="container">窗体或控件容器</param>
/// <param name="model">业务对象</param>
public static void BindControlsToModel(this Control container, EntityBase model)
//public static void BindControlsToModel<T>(this Control container, T model) where T : EntityBase<T>
{
if (model == null) return;
Type objType = model.GetType();
var realContainer = container.GetType().BaseType;
PropertyInfo[] objPropertyArray = objType.GetProperties();

foreach (PropertyInfo propertyInfo in objPropertyArray)
{
//根据对象属性去查找控件
Control control = container.FindControl(propertyInfo.Name);

if (control == null)//无法找到控件 从父类查找
{
try
{
var mem = realContainer.InvokeMember(propertyInfo.Name, BindingFlags.Default | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance
| BindingFlags.GetField | BindingFlags.GetProperty, null, container, null);

if (mem != null)
control = mem as Control;
}
catch
{ }
}

if (control == null) continue;

// ListControl是CheckBoxList、DropDownList、ListBox 和 RadioButtonList 的基类
if (control is ListControl)
{
var listControl = (ListControl)control;
if (listControl.SelectedItem != null)
{
var stringBuilder = new StringBuilder();
for (int i = 0; i < listControl.Items.Count; i++)
{
if (listControl.Items[i].Selected)
{
stringBuilder.Append(listControl.Items[i].Value).Append(",");
}
}

Type propertyType = propertyInfo.PropertyType;

if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
//判断是否为 System.Nullable 类型
propertyType = propertyType.GetGenericArguments()[0];

if (string.IsNullOrEmpty(stringBuilder.ToString()))
{
propertyInfo.SetValue(model, null, null);
continue;
}
}
object objValue = Convert.ChangeType(stringBuilder.ToString().Trim(','), propertyType);
propertyInfo.SetValue(model, objValue, null);
}
}
else
{
Type controlType = control.GetType();
PropertyInfo[] controlPropertyArray = controlType.GetProperties();

GetControlProperty(model, propertyInfo, control, controlPropertyArray);

}
}
}

/// <summary>
/// 将对象值绑定到页面控件(进行中) 要求子控件的名字和对象属性的名字一致
/// </summary>
/// <param name="model">业务对象</param>
/// <param name="container">窗体或控件容器</param>
public static void BindModelToControls(this EntityBase model, Control container)
{
if (model == null) return;
Type objType = model.GetType();
var realContainer = container.GetType().BaseType;
PropertyInfo[] objPropertyArray = objType.GetProperties(BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.NonPublic);
foreach (PropertyInfo propertyInfo in objPropertyArray)
{
//根据对象属性去查找控件
Control control = container.FindControl(propertyInfo.Name);

if (control == null)//无法找到控件 从父类查找
{
try
{
var mem = realContainer.InvokeMember(propertyInfo.Name, BindingFlags.Default | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance
| BindingFlags.GetField | BindingFlags.GetProperty, null, container, null);

if (mem != null)
control = mem as Control;
}
catch
{ }
}

if (control == null) continue;

if (control is ListControl)
{
var listControl = (ListControl)control;
var items = propertyInfo.GetValue(model, null);
if(items==null) continue ;
string propertyValue = items.ToString();
string[] values = propertyValue.Split(',');
if (values.Length == 0) continue;

foreach (string value in values)
{
ListItem listItem = listControl.Items.FindByValue(value);
if (listItem != null)
{
listItem.Selected = true;
}
}
}
else
{
Type controlType = control.GetType();
PropertyInfo[] controlPropertyArray = controlType.GetProperties();
SetControlProperty(model, propertyInfo, control, controlPropertyArray);

}
}
}

/// <summary>
/// 将实体对象的属性值赋值给页面控件
/// </summary>
/// <param name="obj"></param>
/// <param name="objProperty"></param>
/// <param name="control"></param>
/// <param name="controlPropertyArray"></param>
private static void SetControlProperty(object obj, PropertyInfo objProperty, Control control, IEnumerable<PropertyInfo> controlPropertyArray)
{
Dictionary<string, Type> list = GetPropertyList();

foreach (var dict in list)
{
// 在整个控件属性中进行迭代,检查控件匹配名称和类型的属性
var propertys = from p in controlPropertyArray
where p.Name == dict.Key && p.PropertyType == dict.Value
select p;

if (propertys.Count() == 0)
continue;

PropertyInfo controlProperty = propertys.First();

controlProperty.SetValue(control, Convert.ChangeType(objProperty.GetValue(obj, null), dict.Value), null);

}

}

/// <summary>
/// 获取页面控件的值,并赋值给实体对象
/// </summary>
/// <param name="obj"></param>
/// <param name="objProperty"></param>
/// <param name="control"></param>
/// <param name="controlPropertyArray"></param>
private static void GetControlProperty(object obj, PropertyInfo objProperty, Control control, IEnumerable<PropertyInfo> controlPropertyArray)
{
//控件值属性集合及其类型
Dictionary<string, Type> list = GetPropertyList();

foreach (var dict in list)
{
// 检查控件匹配名称和类型的属性
var propertys = from p in controlPropertyArray
where p.Name == dict.Key && p.PropertyType == dict.Value
select p;

if (propertys.Count() == 0)
continue;

PropertyInfo controlProperty = propertys.First();
// 将控件的属性设置为
// 业务对象属性值
Type propertyType = objProperty.PropertyType;

try
{
//判断是否为 System.Nullable 类型
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propertyType = propertyType.GetGenericArguments()[0];

if (string.IsNullOrEmpty(controlProperty.GetValue(control, null).ToString()))
{
//当属性的类型为 System.Nullable,且值为空/null时,设置属性值为null
objProperty.SetValue(obj, null, null);

//赋值成功时,退出循环
break;
}
}

object objValue = Convert.ChangeType(controlProperty.GetValue(control, null), propertyType);
objProperty.SetValue(obj, objValue, null);
//赋值成功时,退出循环
break;

}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
}

private static Dictionary<string, Type> GetPropertyList()
{
var list = new Dictionary<string, Type>();
list.Add("Checked", typeof(bool));
list.Add("Value", typeof(String));
list.Add("Text", typeof(String));
return list;
}

public static void UserDataBind(this BaseDataBoundControl dataListShow)
{
object listDataSource = dataListShow.DataSource;
var type = listDataSource.GetType();
var props = type.GetProperties();
var propParent = type.BaseType.GetProperties();
var propChild = props.Except(propParent);
if(type == typeof(List<>).MakeGenericType())
{
var innerType = type.GetGenericArguments().FirstOrDefault();
var headerRow = type.GetProperty("HeaderRow");

}
}
}
}

posted on 2012-10-30 10:52  hirisw  阅读(243)  评论(0编辑  收藏  举报