zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
  • 相关阅读:
    NFC Basics(基本NFC)——翻译自developer.android.com
    【LeetCode】Sort Colors 解题报告
    发送手机物理标识请求
    Java编程介绍
    emacs 中使用git diff命令行
    OpenJudge百炼习题解答(C++)--题4074:积水量
    编程之美2.9 斐波那契数列
    Application Architecture Determines Application Performance
    程序包javax.servlet.annotation不存在
    mysql 严格模式 Strict Mode说明
  • 原文地址:https://www.cnblogs.com/threadj/p/10535839.html
Copyright © 2011-2022 走看看