如何用反射创建比较器类?

时间:2011-03-24 03:53:31

标签: c# .net

我想创建一个比较器类,它以通用方式比较两个对象。意味着它将检查两个对象中的字段,属性,对象,对象列表。我创建了将两个对象与其公共属性和字段进行比较的代码。但是想要在给定输入对象中比较对象(和对象列表)来添加它。

public static class Comparer<T>
    {
        /// <summary>
        /// Comparer method formcomparing two objects 
        /// </summary>
        /// <param name="x">Object x of type T</param>
        /// <param name="y">Object y of type T</param>
        /// <returns>int value 0/1</returns>
        public static int Compare(T x, T y)
        {
            Type type = typeof(T);

            //Collecting public properties and fields
            PropertyInfo[] properties = type.GetProperties();
            FieldInfo[] fields = type.GetFields();

            int compareValue = 0;

            // Loop for comparing one by one properties values of two objects.
            foreach (PropertyInfo property in properties)
            {
                IComparable valx = property.GetValue(x, null) as IComparable;
                if (valx == null)
                    continue;
                object valy = property.GetValue(y, null);

                compareValue = valx.CompareTo(valy);

                if (compareValue != 0)
                    return compareValue;
            }


            // Loop for comparing one by one fields values of two objects.
            foreach (FieldInfo field in fields)
            {
                IComparable valx = field.GetValue(x) as IComparable;
                if (valx == null)
                    continue;
                object valy = field.GetValue(y);

                compareValue = valx.CompareTo(valy);
                if (compareValue != 0)
                    return compareValue;
            }

            return compareValue;
        }

        /// <summary>
        /// Comparer method for comparing to list objects
        /// </summary>
        /// <param name="x">List object of T type</param>
        /// <param name="y">List object of T type</param>
        /// <returns>Result of comparision as true/false</returns>
        public static bool Compare(List<T> x, List<T> y)
        {
            // Checking input lists as a null.
            if (x == null || y == null)
            {
                return false;
            }

            // Checking input lists count is equal or not.
            if (x.Count() != y.Count())
            {
                return false;
            }

            // Loop that invoke compare method for each of list objects.
            for (int iCntr = 0; iCntr < x.Count(); iCntr++)
            {
                int result = Compare(x[iCntr], y[iCntr]);
                if (result != 0)
                {
                    return false;
                }
            }
            return true;
        }
}

你有什么想法解决这个问题吗?如果您发现有关它的任何参考,请回复我。

此致 Girish

1 个答案:

答案 0 :(得分:0)

您需要为找到的每个属性使用递归。

Pseudocode
public static int Compare(object x, object y)
{
    ... handle if one or both are null
    ... handle if both same type 
    if (IsArray(x))
    {
        ?? equal also means same order??
        for i=0 to min(x.lenght, y.length)-1 {
            int subcompareresult = Compare(x[i], y[i])
            if subcompareresult != 0
                return subcompareresult // difference found
        }

        // elements compared so far are same
        ... handle different length
    } else if IsClass(x)
                foreach subproperty in x.Properties
        int subcompareresult = Compare(x[subproperty ], y[subproperty ])
        if subcompareresult != 0
            return subcompareresult // difference found
            else
        ... handle value compare

注意递归调用比较数组和类中的子项。

您可以查看SharpSerializer源代码。 SharpSerializer将对象图序列化为xml或二进制格式。具有递归遍历的部分可以在中找到 SharpSerializer/.../PropertyFactory.cs