• 通过反射的方法,比较两个相同实体的属性和属性值是否一致


             /// <summary>
            /// 判断两个实体类是否相等
            /// </summary>
            /// <param name="processObj">对象1</param>
            /// <param name="processObj2">对象2</param>
            public static bool ReflectField(object processObj, object processObj2)
            {
                Type t1 = processObj.GetType();
    
                Type t2 = processObj2.GetType();
    
                string str = "";
    
                string str2 = "";
    
                foreach (PropertyInfo item in t1.GetProperties())
                {
                    if (item.Name != "CCBNumber" && item.Name != "CCBCreateDate" && item.Name != "CCBCreaterId" && item.Name != "StampTime" )
                    {
                        str += GetModelValue(item.Name, processObj);
                        Debug.Print(item.Name + ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + GetModelValue(item.Name, processObj));
                    }
                }
    
                foreach (PropertyInfo item in t2.GetProperties())
                {
                    if (item.Name != "CCBNumber" && item.Name != "CCBCreateDate" && item.Name != "CCBCreaterId" && item.Name != "StampTime")
                    {
                        str2 += GetModelValue(item.Name, processObj2);
                        Debug.Print(item.Name + ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + GetModelValue(item.Name, processObj2));
                    }
                }
    
                if (str.Equals(str2))
                {
                    return true;
                }
    
                //str = "";
                //str2 = "";
    
                Debug.Print(str + "--------------------------");
                Debug.Print(str2 + "--------------------------");
    
                return false;
            }
    
            /// <summary>
            /// 获取属性值 
            /// </summary>
            /// <param name="FieldName">属性</param>
            /// <param name="obj">对象</param>
            /// <returns></returns>
            public static string GetModelValue(string FieldName, object obj)
            {
                try
                {
                    Type Ts = obj.GetType();
                    object o = Ts.GetProperty(FieldName).GetValue(obj, null);
                    string Value = Convert.ToString(o);
                    if (string.IsNullOrEmpty(Value)) return null;
                    return Value;
                }
                catch
                {
                    return null;
                }
            }
    http://www.qbzao.com/
  • 相关阅读:
    mac 安装Windows系统
    各种镜像源
    应用官方下载地址汇总
    centos7 升级openssh
    ubuntu16.04升级openssh
    腾讯云
    msdeploy 远程发布到lls
    Java Script 什么是闭包?
    JavaScript我的怀疑
    HTML 之 js是干什么的
  • 原文地址:https://www.cnblogs.com/leaflife/p/7466960.html
Copyright © 2020-2023  润新知