/// <summary> /// IDataRecord到对象的转换 /// </summary> /// <typeparam name="T">需要被转换的类型</typeparam> /// <param name="drs">IDataRecord数组</param> /// <param name="objs">需要被填充的对象列表(类型一致的对象,不允许空引用)</param> /// <param name="columnBindingFlags">BindingFlags</param> public static void TransObject <T>(IDataRecord[] drs, T[] objs, BindingFlags columnBindingFlags) { if (objs == null || objs.Length == 0 || drs == null || drs.Length == 0) { return; } int num = drs.Length; if (objs.Length < num) { num = objs.Length; } Type type = typeof(T); IFieldMemberInfo[] columns = FieldMemberInfo.GetFieldMembers(type, columnBindingFlags); for (int i = 0; i < num; i++) { IDataRecord dr = drs[i]; for (int k = 0; k < columns.Length; k++) { if (columns[k].CanWrite) { columns[k].SetValue(objs[i], dr[columns[k].Name]); } } } }
/// <summary> /// 实体属性到WebForm数据的转换 /// </summary> /// <param name="entity">实体(不允许空引用),支持<see cref="DataRow"/></param> /// <param name="form">被转换的WebForm实体(Page的派生类)</param> /// <example> /// <code> /// public class myPage : System.Web.UI.Page /// { /// [Column("Title")] /// protected TextBox txtTitle; /// [Column("Description")] /// protected TextBox txtDescription; /// /// private void Page_Load(object sender, EventArgs e) { /// YourEntity entity; /// //load entity data ... /// DataTransfer.TransEntityPropertyToWebFormData(entity, this); /// } /// } /// </code> /// </example> public static void TransEntityPropertyToWebFormData(object entity, object form) { if (form == null || entity == null) { return; } Type formType = form.GetType(); Type entityType = entity.GetType(); IFieldMemberInfo[] columns = FieldMemberInfo.GetFieldMembers(formType, BindingFlags.GetField | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, true); for (int i = 0; i < columns.Length; i++) { if (columns[i].CanWrite) { object value = null; if (entity is DataRow) { DataRow dr = (DataRow)entity; value = dr[columns[i].Name]; } else { PropertyInfo property = entityType.GetProperty(columns[i].Name, ColumnBindingFlags); if (property != null && property.CanRead) { value = property.GetValue(entity, null); } } if (value == null) { continue; } object fieldObject = columns[i].GetValue(form); string stringValue = (string)Convert.ChangeType(value, typeof(string)); if (fieldObject is TextBox) { (fieldObject as TextBox).Text = stringValue; } else if (fieldObject is DropDownList) { DropDownList ddl = fieldObject as DropDownList; ddl.ClearSelection(); WebHelper.SetDropDownListValueSafely(ddl, stringValue); } else if (fieldObject is Label) { (fieldObject as Label).Text = stringValue; } else if (fieldObject is HyperLink) { (fieldObject as HyperLink).Text = stringValue; } else { continue; } } } }
public static MemberInfo getInfo(FieldInfo field) { var result = field.getUserData(typeof(FieldMemberInfo)); if (result == null) { result = new FieldMemberInfo(field); field.addUserData(result); } return result; }
public static MemberInfo getInfo(FieldInfo field) { var result = field.getUserData(typeof(FieldMemberInfo)); if (result == null) { result = new FieldMemberInfo(field); field.addUserData(result); } return(result); }
/// <summary> /// 对象到DataTable的转换 /// </summary> /// <param name="obj">对象</param> /// <param name="dr">DataRow</param> public static void TransObject <T>(T obj, DataRow dr) { if (obj == null || dr == null) { return; } Type type = typeof(T); IFieldMemberInfo[] columns = FieldMemberInfo.GetFieldMembers(type, ColumnBindingFlags); for (int k = 0; k < columns.Length; k++) { if (columns[k].CanRead) { dr[columns[k].Name] = columns[k].GetValue(obj); } } }
/// <summary> /// 对象到DataTable的转换 /// </summary> /// <param name="objs">对象列表(类型一致的对象)</param> /// <param name="dt">DataTable</param> /// <param name="columnBindingFlags">BindingFlags</param> public static void TransObject <T>(T[] objs, DataTable dt, BindingFlags columnBindingFlags) { if (objs == null || objs.Length == 0 || dt == null) { return; } Type type = typeof(T); IFieldMemberInfo[] columns = FieldMemberInfo.GetFieldMembers(type, columnBindingFlags); for (int i = 0; i < objs.Length; i++) { DataRow dr = dt.NewRow(); for (int k = 0; k < columns.Length; k++) { if (columns[k].CanRead) { dr[columns[k].Name] = columns[k].GetValue(objs[i]); } } dt.Rows.Add(dr); } }
/// <summary> /// 获取具有 <see cref="ColumnAttribute"/> 属性的缺省值 /// </summary> /// <param name="type">需要反射的类型</param> /// <returns>缺省值列表</returns> public static object[] GetDefaultColumnAttibuteValues(Type type) { IFieldMemberInfo[] members = FieldMemberInfo.GetFieldMembers(type); object[] defaultValues = new object[members.Length]; for (int i = 0; i < members.Length; i++) { IFieldMemberInfo member = members[i]; Type memberType = member.MemberInfo.DeclaringType; object defaultValue = member.Column.DefaultValue; if (defaultValue == null) { defaultValues[i] = memberType.TypeInitializer.Invoke(null); } else { if (memberType == typeof(DateTime)) { switch (defaultValue.ToString()) { case "DateTime.Now": defaultValue = DateTime.Now; break; case "DateTime.MinValue": defaultValue = DateTime.MinValue; break; case "DateTime.MaxValue": defaultValue = DateTime.MaxValue; break; } } defaultValues[i] = defaultValue; } } return(defaultValues); }
/// <summary> /// WebForm数据到实体属性的转换 /// </summary> /// <param name="form">被转换的WebForm实体(Page的派生类)</param> /// <param name="entity">实体(不允许空引用),支持<see cref="DataRow"/>、<see cref="IDataParameterCollection"/></param> /// <example> /// <code> /// public class myPage : System.Web.UI.Page /// { /// [Column("Title")] /// protected TextBox txtTitle; /// [Column("Description")] /// protected TextBox txtDescription; /// /// private void btnSubmit_Click(object sender, EventArgs e) { /// YourEntity entity = new YourEntity(); /// DataTransfer.TransWebFormDataToEntityProperty(this, entity); /// } /// } /// </code> /// </example> public static void TransWebFormDataToEntityProperty(object form, object entity) { if (form == null || entity == null) { return; } Type formType = form.GetType(); IFieldMemberInfo[] columns = FieldMemberInfo.GetFieldMembers(formType, BindingFlags.GetField | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, true); for (int i = 0; i < columns.Length; i++) { if (columns[i].CanRead) { string stringValue; object fieldObject = columns[i].GetValue(form); if (fieldObject is TextBox) { stringValue = (fieldObject as TextBox).Text; } else if (fieldObject is DropDownList) { stringValue = (fieldObject as DropDownList).SelectedValue; } else if (fieldObject is Label) { stringValue = (fieldObject as Label).Text; } else if (fieldObject is HyperLink) { stringValue = (fieldObject as HyperLink).Text; } else { continue; } object value = stringValue; if (columns[i].Column.ColumnType != null) { try { value = Convert.ChangeType(stringValue, columns[i].Column.ColumnType); } catch { if (columns[i].Column.DefaultValue != null) { value = columns[i].Column.DefaultValue; } else { continue; } } } if (entity is DataRow) { DataRow dr = (DataRow)entity; dr[columns[i].Name] = value; } else if (entity is IDataParameterCollection) { IDataParameterCollection pc = (IDataParameterCollection)entity; string pname = columns[i].Name.StartsWith("@") ? columns[i].Name : "@" + columns[i].Name; ((IDataParameter)pc[pname]).Value = value; } else { Type entityType = entity.GetType(); PropertyInfo property = entityType.GetProperty(columns[i].Name, ColumnBindingFlags); if (property != null && property.CanWrite) { property.SetValue(entity, value, null); } } } } }