|
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Xml;
- using System.Xml.Serialization;
-
- namespace CnasSynchronousCommon
- {
- public class CompareObjectOperation
- {
- #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 = ToXMLString(objA);
- string strB = ToXMLString(objB);
- if (strA == strB)
- {
- flag = true;
- }
- }
- return flag;
- }
- #endregion
-
- public static string ToXMLString(object entity)
- {
- using (MemoryStream stream = new MemoryStream())
- {
- XmlTextWriter writer = new XmlTextWriter(stream, null);
- XmlSerializer xml = new XmlSerializer(entity.GetType());
- xml.Serialize(writer, entity);
- writer.Formatting = Formatting.Indented;
- using (StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8))
- {
- stream.Position = 0;
- string xmlString = sr.ReadToEnd();
- sr.Close();
- stream.Close();
- return xmlString;
- }
- }
- }
-
-
- /// <summary>
- /// dataRow比较
- /// </summary>
- /// <param name="drA"></param>
- /// <param name="drB"></param>
- /// <param name="columnNames">需要比较的列名称</param>
- /// <returns></returns>
- public static bool DataRowCompare(DataRow drA, DataRow drB, List<string> columnNames)
- {
- bool flag = true;
- //DataRow 中需要比较的列排序
- ColumnSort(drA, columnNames);
- ColumnSort(drB, columnNames);
- foreach (DataColumn dcA in drA.Table.Columns)
- {
- if (columnNames.Contains(dcA.ColumnName)|| columnNames.Contains(dcA.ColumnName.ToUpper())||columnNames.Contains(dcA.ColumnName.ToLower()))
- {
- foreach (DataColumn dcB in drB.Table.Columns)
- {
- if (columnNames.Contains(dcB.ColumnName)|| columnNames.Contains(dcB.ColumnName.ToUpper())||columnNames.Contains(dcB.ColumnName.ToLower()))
- {
- if (dcB.ColumnName.ToUpper() == dcA.ColumnName.ToUpper())//列名比较
- {
- //类型比较
- if (dcB.DataType != dcA.DataType)
- {
- flag = false;
- break;
- }
- //值比较
- else if (!CompareObject(drA[dcB.ColumnName], drB[dcB.ColumnName]))
- {
- flag = false;
- break;
- }
- }
- }
- }
- }
- }
- return flag;
- }
- /// <summary>
- /// 按照数组中列名顺序排序
- /// </summary>
- /// <param name="drA"></param>
- /// <param name="columnNames">按照数组中列名顺序排序</param>
- public static void ColumnSort(DataRow drA, List<string> columnNames)
- {
- //drA 排序
- int i = 0;
- foreach (string columnName in columnNames)
- {
- if (drA.Table.Columns.Contains(columnName))
- {
- drA.Table.Columns[columnName].SetOrdinal(i);
- i++;
- }
- }
- }
-
- }
- }
|