From: Sean M. <int...@us...> - 2006-02-21 04:31:29
|
Update of /cvsroot/adapdev/Adapdev/src/Adapdev In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9122/src/Adapdev Modified Files: Adapdev.csproj AppDomainManager.cs ObjectComparer.cs Added Files: CollectionSorter.cs Log Message: --- NEW FILE: CollectionSorter.cs --- // Original Copyright (c) 2003 Diego Mijelshon. http://www.codeproject.com/csharp/objectcomparer.asp #region Modified Copyright / License Information /* Copyright 2004 - 2005 Adapdev Technologies, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ============================ Author Log ============================ III Full Name SMM Sean McCormack (Adapdev) ============================ Change Log ============================ III MMDDYY Change */ #endregion using System; using System.Collections; using System.Reflection; namespace Adapdev { [Serializable] public class CollectionSorter : IComparer { #region methods /// <summary> /// Compares two objects and returns a value indicating whether one is less than, equal to or greater than the other. /// </summary> /// <param name="x">First object to compare.</param> /// <param name="y">Second object to compare.</param> /// <returns></returns> public int Compare(object x, object y) { //Get types of the objects Type typex = x.GetType(); Type typey = y.GetType(); for(int i = 0; i<Fields.Length; i++) { //Get each property by name PropertyInfo pix = typex.GetProperty(Fields[i]); PropertyInfo piy = typey.GetProperty(Fields[i]); //Get the value of the property for each object IComparable pvalx = (IComparable)pix.GetValue(x, null); object pvaly = piy.GetValue(y, null); //Compare values, using IComparable interface of the property's type int iResult = pvalx.CompareTo(pvaly); if (iResult != 0) { //Return if not equal if (Descending[i]) { //Invert order return -iResult; } else { return iResult; } } } //Objects have the same sort order return 0; } #endregion #region constructors /// <summary> /// Create a comparer for objects of arbitrary types having using the specified properties /// </summary> /// <param name="fields">Properties to sort objects by</param> public CollectionSorter(params string[] fields) : this(fields, new bool[fields.Length]) {} /// <summary> /// Create a comparer for objects of arbitrary types having using the specified properties and sort order /// </summary> /// <param name="fields">Properties to sort objects by</param> /// <param name="descending">Properties to sort in descending order</param> public CollectionSorter(string[] fields, bool[] descending) { Fields = fields; Descending = descending; } #endregion #region protected fields /// <summary> /// Properties to sort objects by /// </summary> protected string[] Fields; /// <summary> /// Properties to sort in descending order /// </summary> protected bool[] Descending; #endregion } } Index: AppDomainManager.cs =================================================================== RCS file: /cvsroot/adapdev/Adapdev/src/Adapdev/AppDomainManager.cs,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** AppDomainManager.cs 16 Nov 2005 07:02:01 -0000 1.7 --- AppDomainManager.cs 21 Feb 2006 04:31:18 -0000 1.8 *************** *** 36,39 **** --- 36,40 ---- using System.Security.Permissions; using System.Security.Policy; + using Adapdev.Reflection; using SecurityPermission = System.Security.Permissions.SecurityPermission; Index: Adapdev.csproj =================================================================== RCS file: /cvsroot/adapdev/Adapdev/src/Adapdev/Adapdev.csproj,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** Adapdev.csproj 8 Feb 2006 03:00:00 -0000 1.18 --- Adapdev.csproj 21 Feb 2006 04:31:18 -0000 1.19 *************** *** 121,129 **** /> <File - RelPath = "AssemblyCache.cs" - SubType = "Code" - BuildAction = "Compile" - /> - <File RelPath = "AssemblyInfo.cs" SubType = "Code" --- 121,124 ---- *************** *** 316,319 **** --- 311,319 ---- /> <File + RelPath = "Reflection\AssemblyCache.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File RelPath = "Reflection\ClassAccessor.cs" SubType = "Code" *************** *** 351,359 **** /> <File - RelPath = "Reflection\ReflectionCache.cs" - SubType = "Code" - BuildAction = "Compile" - /> - <File RelPath = "Scheduling\Task\ScheduledTasks.cs" SubType = "Code" --- 351,354 ---- Index: ObjectComparer.cs =================================================================== RCS file: /cvsroot/adapdev/Adapdev/src/Adapdev/ObjectComparer.cs,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** ObjectComparer.cs 26 Nov 2005 08:51:25 -0000 1.10 --- ObjectComparer.cs 21 Feb 2006 04:31:18 -0000 1.11 *************** *** 16,30 **** } ! public static bool AreEqual(ISerializable x, ISerializable y) { return AreBytesEqual(Serializer.SerializeToBinary(x), Serializer.SerializeToBinary(y)); } ! public static bool AreEqual(object x, object y) ! { ! return AreEqual(x, y, false); ! } ! ! public static bool AreEqual(object x, object y, bool includeFields) { Type t = x.GetType(); --- 16,38 ---- } ! /// <summary> ! /// Does a binary comparison of the objects. ! /// </summary> ! /// <remarks>Both class must support serialization.</remarks> ! /// <param name="x">Object x.</param> ! /// <param name="y">Object y.</param> ! /// <returns></returns> ! public static bool AreBytesEqual(object x, object y) { return AreBytesEqual(Serializer.SerializeToBinary(x), Serializer.SerializeToBinary(y)); } ! /// <summary> ! /// Are the properties equal. ! /// </summary> ! /// <param name="x">Object x.</param> ! /// <param name="y">Object y.</param> ! /// <returns></returns> ! public static bool ArePropertiesEqual(object x, object y) { Type t = x.GetType(); *************** *** 45,53 **** } - if(includeFields) - { - if(!AreFieldsEqual(x, y)) return false; - } - return true; } --- 53,56 ---- *************** *** 58,70 **** } ! private static bool AreFieldsEqual(object x, object y) { ! foreach(FieldInfo field in x.GetType().GetFields()) { ! if(!(field.GetValue(x).ToString().Equals(field.GetValue(y).ToString()))) return false; } ! return true; } public static bool AreBytesEqual(byte[] a, byte[] b) { --- 61,118 ---- } ! /// <summary> ! /// Are the objects equal. Compares both fields and properties ! /// </summary> ! /// <param name="x">Object x.</param> ! /// <param name="y">Object y.</param> ! /// <returns></returns> ! public static bool AreEqual(object x, object y) { ! bool equal = false; ! equal = ObjectComparer.AreFieldsEqual(x, y); ! if(!equal) return false; ! equal = ObjectComparer.ArePropertiesEqual(x, y); ! return equal; ! } ! ! /// <summary> ! /// Are the fields equal. ! /// </summary> ! /// <param name="x">Object x.</param> ! /// <param name="y">Object y.</param> ! /// <returns></returns> ! public static bool AreFieldsEqual(object x, object y) ! { ! Type t = x.GetType(); ! ClassAccessor accessor = ClassAccessorCache.Get(t); ! try { ! object p1 = null; ! object p2 = null; ! FieldAccessor property = null; ! foreach(string key in accessor.GetFieldAccessors().Keys) ! { ! property = accessor.GetFieldAccessor(key); ! p1 = accessor.GetFieldValue(x, key); ! p2 = accessor.GetFieldValue(y, key); ! ! if(!p1.Equals(Convert.ChangeType(p2, property.FieldType))) ! return false; ! } ! ! return true; } ! catch(ArgumentException) ! { ! return false; ! } } + /// <summary> + /// Are the bytes equal. + /// </summary> + /// <param name="a">Object A</param> + /// <param name="b">Object B</param> + /// <returns></returns> public static bool AreBytesEqual(byte[] a, byte[] b) { |