using System; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Reflection; using System.Xml.Serialization; namespace AIStudio.Wpf.DiagramDesigner { public static class CopyHelper { public static T DeepCopyByReflect(T obj) { //如果是字符串或值类型则直接返回 if (obj == null || obj is string || obj.GetType().IsValueType) return obj; object retval = Activator.CreateInstance(obj.GetType()); FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); foreach (FieldInfo field in fields) { try { field.SetValue(retval, DeepCopyByReflect(field.GetValue(obj))); } catch { } } return (T)retval; } public static TChild AutoCopy(TParent parent) where TChild : TParent, new() { TChild child = new TChild(); var ParentType = typeof(TParent); var Properties = ParentType.GetProperties(); foreach (var Propertie in Properties) { //循环遍历属性 if (Propertie.CanRead && Propertie.CanWrite) { //进行属性拷贝 Propertie.SetValue(child, Propertie.GetValue(parent, null), null); } } return child; } public static T AutoCopy(T source) where T : new() { //如果是字符串或值类型则直接返回 if (source == null || source is string || source.GetType().IsValueType) return source; T target = new T(); var Properties = typeof(T).GetProperties(); foreach (var Propertie in Properties) { //循环遍历属性 if (Propertie.CanRead && Propertie.CanWrite) { //进行属性拷贝 Propertie.SetValue(target, Propertie.GetValue(source, null), null); } } return target; } public static T DeepCopy(T obj) { //如果是字符串或值类型则直接返回 if (obj == null || obj is string || obj.GetType().IsValueType) return obj; object retval; using (MemoryStream ms = new MemoryStream()) { XmlSerializer xml = new XmlSerializer(typeof(T)); xml.Serialize(ms, obj); ms.Seek(0, SeekOrigin.Begin); retval = xml.Deserialize(ms); ms.Close(); } return (T)retval; } /// /// 反射实现两个类的对象之间相同属性的值的复制 /// 适用于初始化新实体 /// /// 返回的实体 /// 数据源实体 /// 数据源实体 /// 返回的新实体 public static D Mapper(S s) { D d = Activator.CreateInstance(); //构造新实例 try { if (s != null) { var Types = s.GetType();//获得类型 var Typed = typeof(D); foreach (PropertyInfo sp in Types.GetProperties().Where(p => p.CanRead))//获得类型的属性字段 { foreach (PropertyInfo dp in Typed.GetProperties().Where(p => p.CanWrite)) { if (dp.Name == sp.Name && dp.PropertyType == sp.PropertyType)//判断属性名是否相同 { dp.SetValue(d, sp.GetValue(s, null), null);//获得s对象属性的值复制给d对象的属性 } } } } } catch (Exception ex) { throw ex; } return d; } public static void CopyPropertyValue(object s, object d, string propertyName = null) { try { var Types = s.GetType();//获得类型 var Typed = d.GetType(); var sps = Types.GetProperties().Where(p => p.CanRead && (string.IsNullOrEmpty(propertyName) || p.Name == propertyName));//获得类型的属性字段 var dps = Typed.GetProperties().Where(p => p.CanWrite && (string.IsNullOrEmpty(propertyName) || p.Name == propertyName)); foreach (PropertyInfo sp in sps)//获得类型的属性字段 { foreach (PropertyInfo dp in dps) { if (dp.Name == sp.Name && dp.PropertyType == sp.PropertyType)//判断属性名是否相同 { dp.SetValue(d, sp.GetValue(s, null), null);//获得s对象属性的值复制给d对象的属性 } } } } catch (Exception ex) { throw ex; } } /// /// 该拷贝不完整,只适应于工具栏使用 /// /// /// /// public static T Mapper(T s) where T : SelectableViewModelBase { if (s == null) return null; T d = Activator.CreateInstance(s.GetType()) as T; d.IsLoaded = false; var properties = s.GetType().GetProperties(); foreach (var propertie in properties) { //循环遍历属性 if (propertie.CanRead && propertie.CanWrite) { //进行属性拷贝 propertie.SetValue(d, propertie.GetValue(s, null), null); } } d.ColorViewModel = CopyHelper.Mapper(s.ColorViewModel); d.FontViewModel = CopyHelper.Mapper(s.FontViewModel); d.ShapeViewModel = CopyHelper.Mapper(s.ShapeViewModel); d.AnimationViewModel = CopyHelper.Mapper(s.AnimationViewModel); d.LockObjectViewModel = CopyHelper.Mapper(s.LockObjectViewModel); d.IsLoaded = true; return d; } public static IColorViewModel Mapper(IColorViewModel s) { var d = CopyHelper.Mapper(s); d.LineColor = CopyHelper.Mapper(s.LineColor); d.FillColor = CopyHelper.Mapper(s.FillColor); d.LineColor.GradientStop = CopyHelper.DeepCopy>(s.LineColor.GradientStop); d.FillColor.GradientStop = CopyHelper.DeepCopy>(s.FillColor.GradientStop); return d; } public static T Mapper(IColorViewModel s) where T: IColorViewModel { var d = CopyHelper.Mapper(s); d.LineColor = CopyHelper.Mapper(s.LineColor); d.FillColor = CopyHelper.Mapper(s.FillColor); d.LineColor.GradientStop = CopyHelper.DeepCopy>(s.LineColor.GradientStop); d.FillColor.GradientStop = CopyHelper.DeepCopy>(s.FillColor.GradientStop); return d; } public static IShapeViewModel Mapper(IShapeViewModel s) { var d = CopyHelper.Mapper(s); d.SourceMarker = CopyHelper.Mapper(s.SourceMarker); d.SinkMarker = CopyHelper.Mapper(s.SinkMarker); return d; } public static T Mapper(IShapeViewModel s) where T : IShapeViewModel { var d = CopyHelper.Mapper(s); d.SourceMarker = CopyHelper.Mapper(s.SourceMarker); d.SinkMarker = CopyHelper.Mapper(s.SinkMarker); return d; } public static IFontViewModel Mapper(IFontViewModel s) { var d = CopyHelper.Mapper(s); return d; } public static T Mapper(IFontViewModel s) where T : IFontViewModel { var d = CopyHelper.Mapper(s); return d; } public static IAnimationViewModel Mapper(IAnimationViewModel s) { var d = CopyHelper.Mapper(s); d.AnimationPath = CopyHelper.Mapper(s.AnimationPath); return d; } public static T Mapper(IAnimationViewModel s) where T : IAnimationViewModel { var d = CopyHelper.Mapper(s); d.AnimationPath = CopyHelper.Mapper(s.AnimationPath); return d; } public static void CopyPropertyValue(IColorViewModel s, IColorViewModel d, string propertyName = null) { if (propertyName == "LineColor") { CopyPropertyValue(s.LineColor, d.LineColor); d.LineColor.GradientStop = CopyHelper.DeepCopy>(s.LineColor.GradientStop); } else if (propertyName == "FillColor") { CopyPropertyValue(s.FillColor, d.FillColor); d.FillColor.GradientStop = CopyHelper.DeepCopy>(s.FillColor.GradientStop); } else { CopyPropertyValue((object)s, (object)d, propertyName); } } } }