通过反射属性判断两个类值是否相同(参考代码)

2015-10-29 17:19:04  访问(1788) 赞(0) 踩(0)


        /// <summary>
        /// 通过反射属性判断两个类值是否相同(参考代码)
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public bool EqualCompare(BaseClassInfo theValue)
        {
            if (theValue == null)
                return false;

            Type valueType = theValue.GetType();
            Type thisType = this.GetType();

            PropertyInfo[] valueA = valueType.GetProperties();
            PropertyInfo[] thisA = thisType.GetProperties();

            if (valueA == null || valueA.Length == 0)
            {
                if (thisA == null || thisA.Length == 0)
                    return true;

                return false;
            }

            if (thisA == null || thisA.Length == 0)
            {
                return false;
            }

            if (valueA.Length != thisA.Length)
            {
                return false;
            }

            PropertyInfo findInfo = null;
            object oValue = null;
            object oThis = null;

            foreach (PropertyInfo infoV in valueA)
            {
                findInfo = null;

                foreach (PropertyInfo thisV in thisA)
                {
                    if (infoV.Name == thisV.Name)
                    {
                        findInfo = thisV;
                        break;
                    }
                }

                if (findInfo == null)
                    return false;

                oValue = infoV.GetValue(theValue, null);
                oThis = findInfo.GetValue(this, null);

                if (oValue == null)
                {
                    if (oThis != null)
                    {
                        return false;
                    }

                    continue;
                }

                if (oThis == null)
                    return false;

                if (!oValue.Equals(oThis))
                    return false;
            }

            return true;
        }


标签:通过反射属性判断两个类值是否相同(参考代码) 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)