/// <summary> /// 为OrmBaseModel的所有同名属性赋值 /// </summary> /// <param name="from">源实例</param> /// <param name="model">OrmBaseModel实例</param> public static void AutoSetOrmModelProperty(object from, OrmBaseModel to) { PropertyInfo[] infos = to.GetType().GetProperties(); foreach (PropertyInfo t_info in infos) { object[] attrs = t_info.GetCustomAttributes(typeof(OrmColumnAttribute), true); if (attrs.Length == 0) { if (t_info.Name != "OrderBy") { continue; } } PropertyInfo f_info = from.GetType().GetProperty(t_info.Name); if (f_info != null) { if (f_info.Name == "OrderBy") { t_info.SetValue(to, f_info.GetValue(from)); } t_info.SetValue(to, f_info.GetValue(from)); } } FieldInfo[] finfos = to.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance); foreach (FieldInfo t_info in finfos) { FieldInfo f_info = from.GetType().GetField(t_info.Name, BindingFlags.NonPublic | BindingFlags.Instance); if (f_info != null) { t_info.SetValue(to, f_info.GetValue(from)); } } }
/// <summary> /// 将DataGridViewRow的一行赋值给OrmModel /// </summary> /// <param name="row"></param> /// <param name="to"></param> public static void AutoSetOrmModelFromDataGridViewRow(DataGridViewRow row, OrmBaseModel to) { PropertyInfo[] infos = to.GetType().GetProperties(); foreach (PropertyInfo t_info in infos) { object[] attrs = t_info.GetCustomAttributes(typeof(OrmColumnAttribute), true); if (attrs.Length == 0) { continue; } try { DataGridViewCell cell = row.Cells["col_" + t_info.Name]; object value = row.Cells["col_" + t_info.Name].Value.ToString(); t_info.SetValue(to, value); } catch (System.ArgumentException ex) { logger.Warn(ex.Message + ":" + ex.StackTrace); } } }