/// <summary> /// IL动态代码(Emit),复制(到已有实体及列表) /// 并行处理列表复制 /// </summary> internal static void CloneObject(object obj, object copy, bool depth) { if (obj is IList list) { var type = list.GenericType(); Delegate action; lock (SyncRoot) { if (!CloneAction.TryGetValue(type.FullName + "." + depth, out action)) { action = CloneBuilder.CloneAction(type, depth); CloneAction.Add(type.FullName + "." + depth, action); } } var copyList = copy as IList; copyList.Clear(); for (int i = 0; i < list.Count; i++) { copyList.Add(Activator.CreateInstance(type)); } Parallel.For(0, list.Count, (i) => { ((Action <object, object>)action)(list[i], copyList[i]); }); } else if (obj != null) { var type = obj.GetType(); if (!type.IsInstanceOfType(copy)) { type = copy.GetType(); } Delegate action; lock (SyncRoot) { if (!CloneAction.TryGetValue(type.FullName + "." + depth, out action)) { action = CloneBuilder.CloneAction(type, depth); CloneAction.Add(type.FullName + "." + depth, action); } } ((Action <object, object>)action)(obj, copy); } }
/// <summary> /// IL动态代码(Emit),复制(实体及列表) /// 并行处理列表复制 /// </summary> internal static object CloneObject(object obj, bool depth) { if (obj is IList list) { var type = list.GenericType(); Delegate action; lock (SyncRoot) { if (!CloneAction.TryGetValue(type.FullName + "." + depth, out action)) { action = CloneBuilder.CloneAction(type, depth); CloneAction.Add(type.FullName + "." + depth, action); } } var iList = type.GenericList(); for (int i = 0; i < list.Count; i++) { iList.Add(Activator.CreateInstance(type)); } Parallel.For(0, list.Count, (i) => { ((Action <object, object>)action)(list[i], iList[i]); }); return(iList); } else if (obj != null) { var type = obj.GetType(); Delegate func; lock (SyncRoot) { if (!CloneFunc.TryGetValue(type.FullName + "." + depth, out func)) { func = CloneBuilder.CloneFunc(type, depth); CloneFunc.Add(type.FullName + "." + depth, func); } } return(((Func <object, object>)func)(obj)); } return(null); }