LINQ 中实现自动表对象装填
设计背景:因做项目使用到了linq,但考虑对表单进行操作时比较麻烦,所以想了一个办法,利用反射自动装填表对象实现更新、插入!
设计背景:因做项目使用到了linq,但考虑对表单进行操作时比较麻烦,所以想了一个办法,利用反射自动装填表对象实现更新、插入!
工作原理:首先遍历出需要的容器控件,然后将控件内容装填到hash表,然后 通过hash表填充表对象
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
/// <summary>
/// 遍历control找到需要的controls
/// </summary>
/// <param name="parent"></param>
/// <param name="ctlForm"></param>
/// <param name="sTarCtlId"></param>
static public void GetConTrolById(Control parent, ref Control ctlForm, string sTarCtlId)
{
if (parent == null) return;
foreach (Control c in parent.Controls)
{
if (CFunc.ToString(c.ID) == sTarCtlId)
{
ctlForm = c;
return;
}
if (c.Controls.Count > 0) GetConTrolById(c, ref ctlForm, sTarCtlId);
}
}
/// <summary>
/// 从表单中获取控件填充至hash
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
static public Hashtable CTGetHash(Control control)
{
try
{
Hashtable hash = new Hashtable();
foreach (Control c in control.Controls)
{
string cType = c.GetType().Name.ToLower();
string cName =CFunc.ToString(c.ID);
if (cName.IndexOf("Hide") > 0)
continue;
switch (cType)
{
case "Label":
break;
case "UltraLabel":
break;
case "Button":
break;
case "webdatechooser":
hash[((WebDateChooser)c).ID] = ((WebDateChooser)c).Value;
break;
case "webnumericedit":
hash[((WebNumericEdit)c).ID] = ((WebNumericEdit)c).Value;
break;
case "textbox":
hash[((TextBox)c).ID] = ((TextBox)c).Text.ToString();
break;
case "dropdownlist":
hash[((DropDownList)c).ID] = ((DropDownList)c).SelectedValue;
break;
case "hiddenfield":
hash[((HiddenField)c).ID] = ((HiddenField)c).Value;
break;
case "checkbox":
if (((CheckBox)c).Checked)
hash[((CheckBox)c).ID] = 1;
else
hash[((CheckBox)c).ID] = 0;
break;
default:
break;
}
}
return hash;
}
catch
{
throw new Exception("从表单中获取hash错误");
}
}
/// <summary>
/// 从Control中将数据填充到实体对象
/// </summary>
/// <param name="entityObject"></param>
/// <param name="control"></param>
/// <returns></returns>
static public T CTControlToEntiry<T>(T entityObject, Control control)
{
Hashtable hash = CTGetHash(control);
Type ty = entityObject.GetType();
System.Reflection.PropertyInfo[] pis = ty.GetProperties();
foreach (System.Reflection.PropertyInfo pi in pis)
{
string propertyName;
propertyName = pi.Name;
if (propertyName.ToLower() == "fid")
continue;
object value = hash[propertyName];
if (value != null)
{
string sFull = pi.PropertyType.FullName.ToString();
if (sFull.IndexOf("String") > 0)
{
pi.SetValue(entityObject, Convert.ToString(value), null);
continue;
}
if (sFull.IndexOf("Int16") > 0)
{
pi.SetValue(entityObject, Convert.ToInt16(value), null);
continue;
}
if (sFull.IndexOf("Int32") > 0)
{
pi.SetValue(entityObject, Convert.ToInt32(value), null);
continue;
}
if (sFull.IndexOf("Int64") > 0)
{
pi.SetValue(entityObject, Convert.ToInt64(value), null);
continue;
}
if (sFull.IndexOf("DateTime") > 0)
{
pi.SetValue(entityObject, Convert.ToDateTime(value), null);
continue;
}
if (sFull.IndexOf("Byte") > 0)
{
pi.SetValue(entityObject, Convert.ToByte(value), null);
continue;
}
if (sFull.IndexOf("Double") > 0)
{
pi.SetValue(entityObject, Convert.ToDouble(value), null);
continue;
}
if (sFull.IndexOf("Single") > 0)
{
pi.SetValue(entityObject, Convert.ToSingle(value), null);
continue;
}
if (sFull.IndexOf("Single") > 0)
{
pi.SetValue(entityObject, Convert.ToSingle(value), null);
continue;
}
}
}
return entityObject;
}
/// <summary>
/// 从hash表填充到对象
/// </summary>
/// <param name="entityObject"></param>
/// <param name="hash"></param>
/// <returns></returns>
static public T CTHasToEntiry<T>(Hashtable hash,T entityObject)
{
Type ty = entityObject.GetType();
System.Reflection.PropertyInfo[] pis = ty.GetProperties();
foreach (System.Reflection.PropertyInfo pi in pis)
{
string propertyName;
propertyName = pi.Name;
if (propertyName.ToLower() == "fid")
continue;
object value = hash[propertyName];
if (value != null)
{
string sFull = pi.PropertyType.FullName.ToString();
if (sFull.IndexOf("String") > 0)
{
pi.SetValue(entityObject, Convert.ToString(value), null);
continue;
}
if (sFull.IndexOf("Int16") > 0)
{
pi.SetValue(entityObject, Convert.ToInt16(value), null);
continue;
}
if (sFull.IndexOf("Int32") > 0)
{
pi.SetValue(entityObject, Convert.ToInt32(value), null);
continue;
}
if (sFull.IndexOf("Int64") > 0)
{
pi.SetValue(entityObject, Convert.ToInt64(value), null);
continue;
}
if (sFull.IndexOf("DateTime") > 0)
{
pi.SetValue(entityObject, Convert.ToDateTime(value), null);
continue;
}
if (sFull.IndexOf("Byte") > 0)
{
pi.SetValue(entityObject, Convert.ToByte(value), null);
continue;
}
if (sFull.IndexOf("Double") > 0)
{
pi.SetValue(entityObject, Convert.ToDouble(value), null);
continue;
}
if (sFull.IndexOf("Single") > 0)
{
pi.SetValue(entityObject, Convert.ToSingle(value), null);
continue;
}
if (sFull.IndexOf("Single") > 0)
{
pi.SetValue(entityObject, Convert.ToSingle(value), null);
continue;
}
}
}
return entityObject;
}
工作原理:首先遍历出需要的容器控件,然后将控件内容装填到hash表,然后 通过hash表填充表对象
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/// <summary>
/// 遍历control找到需要的controls
/// </summary>
/// <param name="parent"></param>
/// <param name="ctlForm"></param>
/// <param name="sTarCtlId"></param>
static public void GetConTrolById(Control parent, ref Control ctlForm, string sTarCtlId)
{
if (parent == null) return;
foreach (Control c in parent.Controls)
{
if (CFunc.ToString(c.ID) == sTarCtlId)
{
ctlForm = c;
return;
}
if (c.Controls.Count > 0) GetConTrolById(c, ref ctlForm, sTarCtlId);
}
}
/// <summary>
/// 从表单中获取控件填充至hash
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
static public Hashtable CTGetHash(Control control)
{
try
{
Hashtable hash = new Hashtable();
foreach (Control c in control.Controls)
{
string cType = c.GetType().Name.ToLower();
string cName =CFunc.ToString(c.ID);
if (cName.IndexOf("Hide") > 0)
continue;
switch (cType)
{
case "Label":
break;
case "UltraLabel":
break;
case "Button":
break;
case "webdatechooser":
hash[((WebDateChooser)c).ID] = ((WebDateChooser)c).Value;
break;
case "webnumericedit":
hash[((WebNumericEdit)c).ID] = ((WebNumericEdit)c).Value;
break;
case "textbox":
hash[((TextBox)c).ID] = ((TextBox)c).Text.ToString();
break;
case "dropdownlist":
hash[((DropDownList)c).ID] = ((DropDownList)c).SelectedValue;
break;
case "hiddenfield":
hash[((HiddenField)c).ID] = ((HiddenField)c).Value;
break;
case "checkbox":
if (((CheckBox)c).Checked)
hash[((CheckBox)c).ID] = 1;
else
hash[((CheckBox)c).ID] = 0;
break;
default:
break;
}
}
return hash;
}
catch
{
throw new Exception("从表单中获取hash错误");
}
}
/// <summary>
/// 从Control中将数据填充到实体对象
/// </summary>
/// <param name="entityObject"></param>
/// <param name="control"></param>
/// <returns></returns>
static public T CTControlToEntiry<T>(T entityObject, Control control)
{
Hashtable hash = CTGetHash(control);
Type ty = entityObject.GetType();
System.Reflection.PropertyInfo[] pis = ty.GetProperties();
foreach (System.Reflection.PropertyInfo pi in pis)
{
string propertyName;
propertyName = pi.Name;
if (propertyName.ToLower() == "fid")
continue;
object value = hash[propertyName];
if (value != null)
{
string sFull = pi.PropertyType.FullName.ToString();
if (sFull.IndexOf("String") > 0)
{
pi.SetValue(entityObject, Convert.ToString(value), null);
continue;
}
if (sFull.IndexOf("Int16") > 0)
{
pi.SetValue(entityObject, Convert.ToInt16(value), null);
continue;
}
if (sFull.IndexOf("Int32") > 0)
{
pi.SetValue(entityObject, Convert.ToInt32(value), null);
continue;
}
if (sFull.IndexOf("Int64") > 0)
{
pi.SetValue(entityObject, Convert.ToInt64(value), null);
continue;
}
if (sFull.IndexOf("DateTime") > 0)
{
pi.SetValue(entityObject, Convert.ToDateTime(value), null);
continue;
}
if (sFull.IndexOf("Byte") > 0)
{
pi.SetValue(entityObject, Convert.ToByte(value), null);
continue;
}
if (sFull.IndexOf("Double") > 0)
{
pi.SetValue(entityObject, Convert.ToDouble(value), null);
continue;
}
if (sFull.IndexOf("Single") > 0)
{
pi.SetValue(entityObject, Convert.ToSingle(value), null);
continue;
}
if (sFull.IndexOf("Single") > 0)
{
pi.SetValue(entityObject, Convert.ToSingle(value), null);
continue;
}
}
}
return entityObject;
}
/// <summary>
/// 从hash表填充到对象
/// </summary>
/// <param name="entityObject"></param>
/// <param name="hash"></param>
/// <returns></returns>
static public T CTHasToEntiry<T>(Hashtable hash,T entityObject)
{
Type ty = entityObject.GetType();
System.Reflection.PropertyInfo[] pis = ty.GetProperties();
foreach (System.Reflection.PropertyInfo pi in pis)
{
string propertyName;
propertyName = pi.Name;
if (propertyName.ToLower() == "fid")
continue;
object value = hash[propertyName];
if (value != null)
{
string sFull = pi.PropertyType.FullName.ToString();
if (sFull.IndexOf("String") > 0)
{
pi.SetValue(entityObject, Convert.ToString(value), null);
continue;
}
if (sFull.IndexOf("Int16") > 0)
{
pi.SetValue(entityObject, Convert.ToInt16(value), null);
continue;
}
if (sFull.IndexOf("Int32") > 0)
{
pi.SetValue(entityObject, Convert.ToInt32(value), null);
continue;
}
if (sFull.IndexOf("Int64") > 0)
{
pi.SetValue(entityObject, Convert.ToInt64(value), null);
continue;
}
if (sFull.IndexOf("DateTime") > 0)
{
pi.SetValue(entityObject, Convert.ToDateTime(value), null);
continue;
}
if (sFull.IndexOf("Byte") > 0)
{
pi.SetValue(entityObject, Convert.ToByte(value), null);
continue;
}
if (sFull.IndexOf("Double") > 0)
{
pi.SetValue(entityObject, Convert.ToDouble(value), null);
continue;
}
if (sFull.IndexOf("Single") > 0)
{
pi.SetValue(entityObject, Convert.ToSingle(value), null);
continue;
}
if (sFull.IndexOf("Single") > 0)
{
pi.SetValue(entityObject, Convert.ToSingle(value), null);
continue;
}
}
}
return entityObject;
}