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;
        }
    }
View Code

相关文章:

  • 2022-12-23
  • 2022-01-25
  • 2021-09-14
  • 2022-01-29
  • 2021-08-09
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-13
  • 2021-12-08
  • 2021-07-31
  • 2021-11-11
  • 2022-01-20
相关资源
相似解决方案