C#利用PropertyInfo反射类实现二个类属性值的比较

来源:互联网 发布:淘宝国产手机 编辑:程序博客网 时间:2024/05/31 06:23

今天的一个项目,需要比较编辑后,修改了哪些字段。通过反射类实现了相同类不同实例之间的属性比较,应该对大家有点用吧。就贡献出来了。当然只是针对我们自己的项目,大家可以修改下适应自己的项目。

        string[] FieldName = new string[] { "DeptID", "Address" };
        string[] RealFieldName = new string [] { "部门ID", "地址" };


        /// <summary>
        /// 返回字段对应真实名,从FieldName找字段名,找出RealFieldName对应位置的真实字段名
        /// </summary>
        /// <param name="GetFieldName">字段名</param>
        /// <returns>真实字段名</returns>
        public string GetRealFieldName(string GetFieldName)
        {
            for(int i=0,count=FieldName.Length;i<count;i++)
            {
                if (FieldName == GetFieldName)
                {
                    return RealFieldName;
                }
                           
            }
            return GetFieldName;//未找到,返回原名


        }


        /// <summary>
        /// 比较二个类的public属性不同,source和dest类型必需相同
        /// </summary>
        /// <param name="source">修改后类</param>
        /// <param name="dest">修改前类</param>
        /// <returns>返回字符型比较结果不同</returns>
        public string ObjectCompare(object source,object dest)
        {
            string ReturnString="";
            PropertyInfo[] pis1 = source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);//获得对象的所有public属性


            if (pis1 != null)//如果获得了属性
            {
                for (int i=0,count=pis1.Length;i<count;i++)//针对每一个属性进行循环
                {
                    object val1, val2;
                    bool CompareTrue;//比较结果
                    string Name;
                    Name =GetRealFieldName(pis1.Name);//获取对应属性名
                    val1 = pis1.GetValue(source, null);//获取源值
                    val2 = pis1.GetValue(dest, null);//获取目标值




                    CompareTrue = true;//默认比较一样
                    if (val1 != null)//可以理解为没有赋值的不进行比较
                    {


                        if (val1.GetType() == typeof(string))//如果是字符型直接比较
                        {
                            string int1, int2;
                            int1 = (string)val1;
                            int2 = (string)val2;
                            if (val1 != val2)
                            {
                                CompareTrue = false;
                            }
                        }
                        else if (val1.GetType() == typeof(int))//如果是数字型直接比较
                        {
                            int int1, int2;
                            int1 = (int)val1;
                            int2 = (int)val2;
                            if (int1 != int2)
                            {
                                CompareTrue = false;
                            }
                        }
                        else if (val1.GetType() == typeof(DateTime))//如果是时间型直接比较
                        {
                            DateTime int1, int2;
                            int1 = (DateTime)val1;
                            int2 = (DateTime)val2;
                            if (int1 != int2)
                            {
                                CompareTrue = false;


                            }
                        }
                        else if (val1.GetType() == typeof(byte))//如果是字节直接比较
                        {
                            byte int1, int2;
                            int1 = (byte)val1;
                            int2 = (byte)val2;
                            if (int1 != int2)
                            {
                                CompareTrue = false;
                            }
                        }
                        else if (val1.GetType() == typeof(bool))//如果是BOOL直接比较
                        {
                            bool int1, int2;
                            int1 = (bool)val1;
                            int2 = (bool)val2;
                            if (int1 != int2)
                            {
                                CompareTrue = false;
                            }
                        }
                        else//其他类型不比较
                        {
                            continue;
                        }
                        if (CompareTrue == false)//值不同
                        {
                            if (val1.GetType() == typeof(bool))//如果是BOOL
                            {
                                if ((bool)val1 == false)
                                    ReturnString += "把" + Name + "从是修改为否;";
                                else
                                    ReturnString += "把" + Name + "从否修改为是;";
                            }
                            else
                            {
                                ReturnString += "把" + Name + "从" + val2.ToString() + "修改为" + val1.ToString() + ";";
                            }
                        }
                    }
                }
            }
            return ReturnString;


        }

原创粉丝点击