public class BaseEntity<T> where T : struct { public T ID { get; set; } public void Clone(BaseEntity<T> target) { target = (BaseEntity<T>)this.MemberwiseClone(); } /// <summary> /// 对象间钱拷贝,target为空是,创建新对象 /// </summary> /// <param name="source">源</param> /// <param name="target">目标</param> /// <returns></returns> public BaseEntity<T> Copy(BaseEntity<T> target = null, string[] excludeProperties = null) { if (null == target) { target = new BaseEntity<T>(); } var type = this.GetType(); var properties = type.GetProperties(); var targetType = target.GetType(); var targetProperties = targetType.GetProperties(); foreach (var property in properties) { if (null != excludeProperties && excludeProperties.Contains(property.Name)) { //不处理 } else { if (targetProperties.Select(x => x.Name).Contains(property.Name)) { if (property.CanWrite) { property.SetValue(target, property.GetValue(this, null), null); } } } } return target; } }
相关文章: