C#反射——模仿BeanUtil属性复制

反射工具类请参见:https://www.cnblogs.com/threadj/p/10535796.html

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;

namespace ReligionServer.util {
    public class BeanUtil {

        /// <summary>
        /// 两个对象的相同属性复制, 会将Source中与Target属性名相同的属性赋值(且属性类型相同), target中别的属性值不会被破坏
        /// </summary>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public static Object PropCopy(Object source, Object target) {

            //在后来的修改之后, 返回List, 且包含当前类和除开Object的所有父类的属性
            //List<FieldInfo> sourceFields = ReflectionUtil.GetFileldS(source.GetType());
            //List<FieldInfo> targetFields = ReflectionUtil.GetFileldS(target.GetType());

            foreach (FieldInfo sourceItem in ReflectionUtil.GetFileldS(source.GetType())) {
                foreach (FieldInfo targetItem in ReflectionUtil.GetFileldS(target.GetType())) {
                    if (sourceItem.Name.Equals(targetItem.Name) && sourceItem.FieldType.Equals(targetItem.FieldType)) {
                        //targetItem.SetValue(target, sourceItem.GetValue(source).ToString().Trim());//可能出现空指针, 字段的类型也应该保持一致, 所以不应该ToString 和 Trim
                        targetItem.SetValue(target, sourceItem.GetValue(source));
                        break;
                    }
                }
            }
            return target;
        }
        /// <summary>
        /// 两个对象的相同属性复制, 会将Source中与Target属性名相同的属性赋值(且属性类型相同), target中别的属性值不会被破坏
        /// 如果属性值相同的则不进行复制, withOutNull参数为true, 那么source中为null或者为""的属性则不复制,
        /// 暂时没有测试
        /// </summary>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <param name="withOutNull"></param>
        /// <returns></returns>
        public static Object PropCopyWithOutSame(Object source, Object target, bool withOutNull) {

            //在后来的修改之后, 返回List, 且包含当前类和除开Object的所有父类的属性
            //List<FieldInfo> sourceFields = ReflectionUtil.GetFileldS(source.GetType());
            //List<FieldInfo> targetFields = ReflectionUtil.GetFileldS(target.GetType());

            foreach (FieldInfo sourceItem in ReflectionUtil.GetFileldS(source.GetType())) {
                foreach (FieldInfo targetItem in ReflectionUtil.GetFileldS(target.GetType())) {
                    if (sourceItem.Name.Equals(targetItem.Name) && sourceItem.FieldType.Equals(targetItem.FieldType)) {
                        //targetItem.SetValue(target, sourceItem.GetValue(source).ToString().Trim());//可能出现空指针, 字段的类型也应该保持一致, 所以不应该ToString 和 Trim
                        if (sourceItem.GetValue(source) != targetItem.GetValue(target)) {//这里判断相等不能使用Equals, 因为很有可能出现空指针异常
                            if (withOutNull) {
                                bool flag = IsEmpty(sourceItem.FieldType, sourceItem.GetValue(source));
                                if (!flag) {
                                    targetItem.SetValue(target, sourceItem.GetValue(source));
                                }
                            } else {
                                targetItem.SetValue(target, sourceItem.GetValue(source));
                            }
                            break;
                        }
                    }
                }
            }
            return target;
        }


        public static bool IsEmpty(Type type, Object value) {
            bool flag = true;
            if (type.Equals(typeof(DateTime))) {
                flag = value == null;
            } else if (type.Equals(typeof(MongoDB.Bson.BsonValue))) {
                flag = MongoDB.Bson.BsonValue.Create(value) == null;
            } else if (type.Equals(typeof(String))) {
                flag = CommonUtil.IsEmpty(Convert.ToString(value));
            } else {
                flag = value == null;
            }
            return flag;
        }
    }
}

 

posted @ 2019-03-15 11:18  边见众生,边见自己  阅读(625)  评论(0编辑  收藏  举报