private static void DoMergeSubClasses(ORMapping child, ORMapping parent, Collection <string> properties) { if (child.SubClasses == null) { child.SubClasses = new Collection <ManyToSubClassType>(); } else { foreach (ManyToSubClassType subObj in child.SubClasses) { properties.Add(subObj.Property); } } foreach (ManyToSubClassType subObj in parent.SubClasses) { if (!properties.Contains(subObj.Property)) { child.SubClasses.Add(subObj.Clone()); } } }
private static void DoMergeObjectClasses(ORMapping child, ORMapping parent, Collection <string> properties) { if (child.ObjectClasses == null) { child.ObjectClasses = new Collection <ManyToObjectClassType>(); } else { foreach (ManyToObjectClassType objClass in child.ObjectClasses) { properties.Add(objClass.Property); } } foreach (ManyToObjectClassType objClass in parent.ObjectClasses) { if (!properties.Contains(objClass.Property)) { child.ObjectClasses.Add(objClass.Clone()); } } }
private static void DoMergeSates(ORMapping child, ORMapping parent, Collection <string> properties) { if (child.States == null) { child.States = new Collection <OneToStateType>(); } else { foreach (OneToStateType state in child.States) { properties.Add(state.Property); } } foreach (OneToStateType state in parent.States) { if (!properties.Contains(state.Property)) { child.States.Add(state.Clone()); } } }
private static void DoMergeDefaultValues(ORMapping child, ORMapping parent) { Collection <string> defValues = new Collection <string>(); if (child.DefaultValues == null) { child.DefaultValues = new Collection <DefaultValueType>(); } else { foreach (DefaultValueType defValue in child.DefaultValues) { defValues.Add(defValue.Column); } } foreach (DefaultValueType defValue in parent.DefaultValues) { if (!defValues.Contains(defValue.Column)) { child.DefaultValues.Add(defValue.Clone()); } } }
/// <summary> /// 用对象属性给DataRow中对应的字段赋值 /// </summary> /// <param name="targetRow">需要赋值的数据行</param> /// <param name="obj">用来赋值的对象</param> /// <param name="colMaps">如果要使用关联类的属性来赋值,则需要提供当前数据集与类默认数据集的字段映射关系</param> public static void SetDataRowValueFromObject(DataRow targetRow, object obj, Collection <ColumnToColumn> colMaps) { if (obj == null) { throw new ArgumentNullException(); } if (targetRow == null) { throw new ArgumentNullException(); } // 根据传入对象名创建实例 Type objType = obj.GetType(); ORMapping orm = FindORMapping(CastClassNameFromFullName(objType.FullName)); if (orm == null) { throw new ArgumentOutOfRangeException("未找到指定类的定义"); } // 获取类所有Public属性 PropertyInfo[] properties = GetProperties(objType); // 简化处理:先统一赋字段的默认值,再根据属性赋值 if (orm.DefaultValues != null) { foreach (DefaultValueType defVal in orm.DefaultValues) { if (targetRow.Table.Columns.Contains(defVal.Column)) { targetRow[defVal.Column] = defVal.Value; } } } Dictionary <string, string> colMapDic = new Dictionary <string, string>(); // 原始表中列名-当前表中列名 if (colMaps != null) { foreach (ColumnToColumn colCol in colMaps) { if (!String.IsNullOrEmpty(colCol.SourceColumn)) { colMapDic.Add(colCol.TargetColumn, colCol.SourceColumn); } } } // 给字段赋值 if (orm.OneOnes != null) { foreach (OneToOneType one in orm.OneOnes) { SetColumnValueFromNormal(targetRow, obj, properties, one, colMapDic); } } if (orm.States != null) { foreach (OneToStateType state in orm.States) { SetColumnValueFromState(targetRow, obj, properties, state, colMapDic); } } if (orm.Structures != null) { foreach (ManyToStructureType structure in orm.Structures) { SetColumnValueFromStruct(targetRow, obj, properties, structure, colMapDic); } } if (orm.ObjectClasses != null) { foreach (ManyToObjectClassType objClass in orm.ObjectClasses) { SetColumnValueFromLinkObject(targetRow, obj, properties, objClass, colMapDic); } } if (orm.SubClasses != null) { foreach (ManyToSubClassType subObj in orm.SubClasses) { SetColumnValueFromSubClass(targetRow, obj, properties, subObj); } } }
private static void SetObjectPropertyFromDataRow(object targetObj, DataRow sourceRow, Collection <ColumnToColumn> colMaps) { try { if (targetObj == null) { return; } Type objType = targetObj.GetType(); ORMapping orm = FindORMapping(CastClassNameFromFullName(objType.FullName)); if (orm == null) { throw new ArgumentOutOfRangeException("未找到指定类的定义"); } // 获取类所有Public属性 PropertyInfo[] properties = GetProperties(objType); // 如果有BeginInit方法则调用 MethodInfo beginInitMethod = GetMethod(objType, "BeginInit"); if (beginInitMethod != null) { beginInitMethod.Invoke(targetObj, null); } Dictionary <string, ColumnToColumn> colMapDic = new Dictionary <string, ColumnToColumn>(); // 原始表中列名-当前表中列名 if (colMaps != null) { for (int i = 0; i < colMaps.Count; i++) { if (!colMapDic.ContainsKey(colMaps[i].TargetColumn))//先判断是否添加了相同的KEY { colMapDic.Add(colMaps[i].TargetColumn, colMaps[i]); } } } // 给属性赋值 if (orm.OneOnes != null) { foreach (OneToOneType oneOne in orm.OneOnes) { SetPropertyValueOfNormal(targetObj, properties, sourceRow, oneOne, colMapDic); } } if (orm.States != null) { foreach (OneToStateType state in orm.States) { SetPropertyValueOfState(targetObj, properties, sourceRow, state, colMapDic); } } if (orm.Structures != null) { foreach (ManyToStructureType structure in orm.Structures) { SetPropertyValueOfStruct(targetObj, properties, sourceRow, structure, colMapDic); } } if (orm.ObjectClasses != null) { foreach (ManyToObjectClassType objClass in orm.ObjectClasses) { SetPropertyValueOfLinkObject(targetObj, properties, sourceRow, objClass, colMapDic); } } if (orm.SubClasses != null) { foreach (ManyToSubClassType subObj in orm.SubClasses) { SetPropertyValueOfSubClass(targetObj, properties, sourceRow, subObj); } } // 如果有EndInit方法则调用 MethodInfo endInitMethod = GetMethod(objType, "EndInit"); if (endInitMethod != null) { endInitMethod.Invoke(targetObj, null); } } catch (Exception ex) { //MessageBox.Show("SetObjectPropertyFromDataRow方法出错:" + ex.Message); //throw; } }