繁体   English   中英

在C#中实现通用对象比较

[英]Implement generic objects comparing in C#

我正在实现通用对象比较方法来比较项目中类的实例。 在每个类中,我都有一些值类型变量和一个关联类的绑定列表。 使用值类型变量,我可以使用==运算符或equal运算符来比较它,但是对于bindinglist,我不知道如何将它转换为bindinglist<type of associate class>来迭代它并执行递归。

public bool IsEqual<T>(T obj1, T obj2)
{
    PropertyInfo[] prop1 = obj1.GetType().GetProperties();
    PropertyInfo[] prop2 = obj2.GetType().GetProperties(); 

    for(int i = 0; i < prop1.Count; i++)
    {
        if(prop1[i].IsValueType && prop2[i].IsValueType)
        {
            if(prop1.GetValue(i) != prop2.GetValue(i))
                return false
        }
        else
        {
            //This is bindinglist of associate class
            //I need to cast it to iterate in perform recursion here            
        }
    }

    return true
}

那么当属性是绑定列表时,如何实现递归呢?

P / S:原谅我糟糕的英语

更新:

仔细考虑后,我实施了IEqualtable正如Stephen Hewlett先生所建议的那样。 非常感谢,Stephen Hewlett先生。 对于那些仍然想要使用比较功能的人,我会给你一种方法,我认为它会起作用:

public bool IsEqual(Object obj1, Object obj2)
{
    PropertyInfo[] prop1 = obj1.GetType().GetProperties();
    PropertyInfo[] prop2 = obj2.GetType().GetProperties(); 

    for(int i = 0; i < prop1.Count; i++)
    {
        if(prop1[i].IsValueType && prop2[i].IsValueType)
        {
            if(prop1[i].GetValue(obj1, null) != prop2[i].GetValue(obj2, null))
                return false;
        }
        else if (prop1[i].PropertyType.IsGenericType && prop2[i].PropertyType.IsGenericType) //if property is a generic list
        {
            //Get actual type of property
            Type type = prop1[i].PropertyType;

            //Cast property into type
            var list1 = Convert.ChangeType(prop1[i].GetValue(obj1, null), type);
            var list2 = Convert.ChangeType(prop1[i].GetValue(obj2, null), type);

            if (list1.count != list2.count)
                return false;

            for j as integer = 0 to list1.Count - 1
            {
                //Recursion here
                if (!IsEqual(list1(j), list2(j)))
                {
                    return false;
                }
            }
        }
        else //if property is instance of a class
        {
            Type type = prop1[i].PropertyType;
            Object object1 = Convert.ChangeType(prop1[i].GetValue(obj1, null), type);
            Object object2 = Convert.ChangeType(prop1[i].GetValue(obj2, null), type);

            //Recursion
            if(!IsEqual(object1, object2))
            {
                 return false;
            }
        }
    }

    return true;
}

非常非常强烈建议为你的所有类实现IEquatable - 不管它们有多少 - 因为对不需要它的东西使用反射通常是一个坏主意。 C#的一个基本优势是通过使用所有内置类型功能获得的类型安全性。 通过使用反射,你试图模仿C#和.NET已经为你做的事情,你可能会弄错。

您的方法不需要是通用的,因为它使用反射,因此所讨论的object无论如何都将作为object传递给反射方法。 添加类型参数没有任何好处。 如果没有type参数,您可以更轻松地根据需要强制转换绑定列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM