/// <summary> /// 将源类型的属性值转换给目标类型同名的属性,排除要过滤的属性名称 /// </summary> /// <param name="source"></param> /// <param name="target"></param> public void Cast(object source, object target, string[] filter) { if (source == null) { throw new ArgumentNullException("source"); } if (target == null) { throw new ArgumentNullException("target"); } for (int i = 0; i < mProperties.Count; i++) { CastProperty cp = mProperties[i]; if (cp.SourceProperty.Getter != null) { object Value = cp.SourceProperty.Getter(source, null); //PropertyInfo.GetValue(source,null); if (cp.TargetProperty.Setter != null) { if (filter == null) { cp.TargetProperty.Setter(target, Value, null); } else if (!filter.Contains(cp.TargetProperty.PropertyName)) { cp.TargetProperty.Setter(target, Value, null); } } } } }
public void Copy(object source, object target) { for (int i = 0; i < mProperties.Count; i++) { CastProperty cp = mProperties[i]; cp.TargetProperty.Set(target, cp.SourceProperty.Get(source)); } }
/// <summary> /// 将源类型的属性值转换给目标类型同名的属性,排除要过滤的属性名称 /// </summary> /// <param name="source"></param> /// <param name="target"></param> /// <param name="filter">要过滤的属性名称</param> public void Cast(object source, object target, string[] filter) { if (source == null) { throw new ArgumentNullException("source"); } if (target == null) { throw new ArgumentNullException("target"); } if (source is EntityBase) { if (filter == null) { filter = new string[] { } } ; var list = filter.ToList(); list.Add("IdentityName"); list.Add("PrimaryKeys"); list.Add("PropertyNames"); list.Add("PropertyValues"); list.Add("TableName"); list.Add("Item"); filter = list.ToArray(); } PropertyAccessorHandler ctp = null; for (int i = 0; i < mProperties.Count; i++) { CastProperty cp = mProperties[i]; if (cp.SourceProperty.Getter != null) { ctp = cp.TargetProperty; if (ctp.Setter != null) { if (filter == null) { object Value = cp.SourceProperty.Getter(source, null); ctp.Setter(target, Value, null); } else { if (!filter.Contains(ctp.PropertyName)) { object Value = cp.SourceProperty.Getter(source, null); ctp.Setter(target, Value, null); } } } } } }
/// <summary> /// 以两个要转换的类型作为构造函数,构造一个对应的转换类 /// </summary> /// <param name="targetType"></param> public ModuleCast(Type targetType) { PropertyInfo[] targetProperties = targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo tp in targetProperties) { CastProperty cp = new CastProperty(); cp.TargetProperty = new PropertyAccessorHandler(tp); mProperties.Add(cp); } }
public ObjectCopy(Type sourceType, Type targetType) { foreach (PropertyInfo sp in sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { foreach (PropertyInfo tp in targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (sp.Name == tp.Name && sp.PropertyType == tp.PropertyType) { CastProperty cp = new CastProperty(); cp.SourceProperty = new PropertyHandler(sp); cp.TargetProperty = new PropertyHandler(tp); mProperties.Add(cp); } } } }
public ModuleCast(Type sourceType, Type targetType) { foreach (PropertyInfo sp in sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { foreach (PropertyInfo tp in targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (sp.Name == tp.Name && sp.PropertyType == tp.PropertyType) { CastProperty cp = new CastProperty(); cp.SourceProperty = new PropertyHandler(sp); cp.TargetProperty = new PropertyHandler(tp); mProperties.Add(cp); } } } }
/// <summary> /// 以两个要转换的类型作为构造函数,构造一个对应的转换类 /// </summary> /// <param name="sourceType"></param> /// <param name="targetType"></param> public ModelCast(Type sourceType, Type targetType) { PropertyInfo[] targetProperties = targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo sp in sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { foreach (PropertyInfo tp in targetProperties) { if (sp.Name == tp.Name && sp.PropertyType == tp.PropertyType) { CastProperty cp = new CastProperty(); cp.SourceProperty = new PropertyAccessorHandler(sp); cp.TargetProperty = new PropertyAccessorHandler(tp); mProperties.Add(cp); break; } } } }
/// <summary> /// 将源类型的属性值转换给目标类型同名的属性 /// </summary> /// <param name="source"></param> /// <param name="target"></param> public void Cast(object source, object target) { if (source == null) { throw new ArgumentNullException("source"); } if (target == null) { throw new ArgumentNullException("target"); } for (int i = 0; i < mProperties.Count; i++) { CastProperty cp = mProperties[i]; if (cp.SourceProperty.Getter != null) { object Value = cp.SourceProperty.Getter(source, null); if (cp.TargetProperty.Setter != null) { cp.TargetProperty.Setter(target, Value, null); } } } }
set => SetValue(CastProperty, value);