首页 >后端开发 >C#.Net教程 >C# 对象比较(值类型、引用类型)

C# 对象比较(值类型、引用类型)

黄舟
黄舟原创
2017-02-15 11:12:241569浏览

<pre class="csharp"><pre class="csharp">        #region 引用对象比较
        /// <summary>
        /// 引用对象比较
        /// </summary>
        /// <param name="objA"></param>
        /// <param name="objB"></param>
        /// <returns></returns>
        public static bool CompareObject(object objA, object objB)
        {
            bool flag = false;
            if (objA == null || objB == null)
            {
                flag = false;
            }
            else if (objA == DBNull.Value && objB != DBNull.Value)
            {
                flag = false;
            }
            else if (objA != DBNull.Value && objB == DBNull.Value)
            {
                flag = false;
            }
            else if (objA == DBNull.Value && objB == DBNull.Value)
            {
                //objA objB 对应的列类型已经比较过 类型已判断 值一致
                flag = true;
            }
            else if (objA.GetType() != objB.GetType())
            {
                flag = false;
            }
            else if (objA is int || objA is short || objA is long || objA is float || objA is double || objA is decimal)
            {
                //int 01与1      
                if (objA is int)
                {
                    if ((int)objA == (int)objB)
                    {
                        flag = true;
                    }
                }
                else if (objA is short)
                {
                    if ((short)objA == (short)objB)
                    {
                        flag = true;
                    }
                }
                else if (objA is long)
                {
                    if ((long)objA == (long)objB)
                    {
                        flag = true;
                    }
                }
                else if (objA is float)
                {
                    if ((float)objA == (float)objB)
                    {
                        flag = true;
                    }
                }
                else if (objA is double)
                {
                    if ((double)objA == (double)objB)
                    {
                        flag = true;
                    }
                }
                else if (objA is decimal)
                {
                    if ((decimal)objA == (decimal)objB)
                    {
                        flag = true;
                    }
                }
            }
            else
            {
                string strA = MetadataXmlSerializer<object>.ToXMLString(objA);
                string strB = MetadataXmlSerializer<object>.ToXMLString(objB);
                if (strA == strB)
                {
                    flag = true;
                }
            }
            return flag;
        }
        #endregion



小注:

如果传入的两个值是dataRow中单元格的值,请先比较类型,类型一致再调用该方法

深拷贝部分代码:

C# 实体类序列化与反序列化一 (XmlSerializer)

C# 实体类序列化与反序列化二 (DataContractSerializer)

以上就是C#  对象比较(值类型、引用类型)的内容,更多相关内容请关注PHP中文网(www.php.cn)!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn