/// <summary> /// 复制对象 /// </summary> /// <returns></returns> public new ManyToObjectClassType Clone() { ManyToObjectClassType obj = new ManyToObjectClassType(); obj.Property = Property; obj.ClassName = ClassName; obj.KindColumn = KindColumn; obj.LinkedObjects = new Collection <LinkedObject>(); foreach (LinkedObject link in LinkedObjects) { obj.LinkedObjects.Add(link.Clone()); } return(obj); }
private static void SetColumnValueFromLinkObject(DataRow targetRow, object obj, PropertyInfo[] properties, ManyToObjectClassType objClass, Dictionary <string, string> colMaps) { object linkObj = GetPropertyValue(obj, properties, objClass.Property); if (linkObj == null) { return; } Type objType = linkObj.GetType(); foreach (LinkedObject link in objClass.LinkedObjects) { if (link.ClassName == CastClassNameFromFullName(objType.FullName)) { Collection <ColumnToColumn> newMaps; if ((colMaps != null) && (colMaps.Count > 0)) { newMaps = new Collection <ColumnToColumn>(); ColumnToColumn newCol2Col; foreach (ColumnToColumn colMap in link.ColumnToColumns) { newCol2Col = new ColumnToColumn(); newCol2Col.TargetColumn = colMap.TargetColumn; if (colMaps.ContainsKey(colMap.SourceColumn)) { newCol2Col.SourceColumn = colMaps[colMap.SourceColumn]; } else { newCol2Col.SourceColumn = colMap.SourceColumn; } newMaps.Add(newCol2Col); } } else { newMaps = link.ColumnToColumns; } SetDataRowValueFromObject(targetRow, linkObj, newMaps); } } }
/// <summary> /// 二次修改 TYR Catch /// add by ywk 2013年3月13日10:22:29 /// </summary> /// <param name="obj"></param> /// <param name="properties"></param> /// <param name="sourceRow"></param> /// <param name="objClass"></param> /// <param name="colMaps"></param> private static void SetPropertyValueOfLinkObject(object obj, PropertyInfo[] properties, DataRow sourceRow, ManyToObjectClassType objClass, Dictionary <string, ColumnToColumn> colMaps) { try { // 使用类名或用工厂类取得实际的类名 string className; if (String.IsNullOrEmpty(objClass.KindColumn)) { className = objClass.ClassName; } else { throw new ArgumentException(MessageStringManager.GetString("ClassNotImplement")); } object newObj = null; // 某些情况下在同一张表中用相同的字段保存不同情况下的数据,然后用某个标志位进行判断当前的数据表示何种情况。 // 在映射到对象时,每种情况都可能是一种单独的类。所以在配置中会将列和不同对象进行映射的关系都给出,由标志字段的值来决定取哪一种配置。 foreach (LinkedObject linkObj in objClass.LinkedObjects) { if ((String.IsNullOrEmpty(className)) || (linkObj.ClassName == className)) { // 有可能存在级联的情况(表的一组列关联到Link对象,而Link对象中的部分字段又关联到另外的对象) // ,所以要得到最终的原始列和关联列的关系( a -> b, b -> c ==> a -> c) Collection <ColumnToColumn> newMaps; if ((colMaps != null) && (colMaps.Count > 0)) { newMaps = new Collection <ColumnToColumn>(); ColumnToColumn newCol2Col; foreach (ColumnToColumn linkColMap in linkObj.ColumnToColumns) { newCol2Col = new ColumnToColumn(); newCol2Col.TargetColumn = linkColMap.TargetColumn;// 第三个表的列名 if (colMaps.ContainsKey(linkColMap.SourceColumn)) { newCol2Col.SourceColumn = colMaps[linkColMap.SourceColumn].SourceColumn; newCol2Col.DefaultValue = colMaps[linkColMap.SourceColumn].DefaultValue; } else if (!String.IsNullOrEmpty(linkColMap.DefaultValue)) { // 中间断开了,则表示不能建立级联关系 newCol2Col.SourceColumn = ""; // linkColMap.SourceColumn; newCol2Col.DefaultValue = linkColMap.DefaultValue; } else { continue; } newMaps.Add(newCol2Col); } } else { newMaps = linkObj.ColumnToColumns; } newObj = CreateAndIntializeObject(linkObj.ClassName, sourceRow, newMaps); break; } } SetPropertyValue(obj, properties, newObj, objClass.Property); } catch (Exception ex) { //MessageBox.Show("SetPropertyValueOfLinkObject方法出错" + ex.Message); } }