• 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;
            }
        }
    }
  • 相关阅读:
    Android 接口中含有" "不能正常显示
    android
    EditText禁止自动弹出键盘
    相册选择照片
    Android完美获取状态栏高度、标题栏高度、编辑区域高度的获取
    Android WebView获取UserAgent
    php extract()用法
    php的安装和破解
    php 兼容换行符
    php 引用
  • 原文地址:https://www.cnblogs.com/threadj/p/10535839.html
Copyright © 2020-2023  润新知