Click here to Skip to main content
16,018,418 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'd like to compare the data types of two classes and return bool values. The problem is my method doesn't compare values inside class of a class

Here is the code:

C#
public static class Compare
{
    public static bool PublicInstancePropertiesEqual<T>(this T self, T to, params string[] ignore) where T : class
    {
        if (self != null && to != null)
        {
            var type = typeof(T);
            var ignoreList = new List<string>(ignore);
            var unequalProperties =
                from pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                where !ignoreList.Contains(pi.Name)
                let selfValue = type.GetProperty(pi.Name).GetValue(self, null)
                let toValue = type.GetProperty(pi.Name).GetValue(to, null)
                where selfValue != toValue && (selfValue == null || !PublicInstancePropertiesEqual(selfValue, toValue, ignore))
                select selfValue;
            return !unequalProperties.Any();
        }
        return self == to;
    }
}


And here is the comparison:

C#
private void Form1_Load(object sender, EventArgs e)
    {
        Obj1 obj1 = new Obj1();
        Obj1 obj11 = new Obj1();
        Obj2 obj2 = new Obj2();
        Obj2 obj22 = new Obj2();

        obj1.param1 = "1";
        obj1.param2 = "2";

        obj2.param3 = "3";
        obj1.obj2 = obj2;

        obj11.param1 = "1";
        obj11.param2 = "2";
        obj22.param3 = "4";
        obj11.obj2 = obj22;
        bool res = Compare.PublicInstancePropertiesEqual(obj1, obj11, ("secure"));

    }
}
class Obj1
{
    public string param1 { get; set; }
    public string param2 { get; set; }
    public Obj2 obj2 { get; set; }
}
class Obj2
{
    public string param3 { get; set; }
    public decimal param4 { get; set; }

}


The returned value is res is true even when the values of the parameters change.
Posted

1 solution

Start by ditching the LINQ stuff. Rewrite in normal statements so you can debug what's going on. Here's a start:
C#
public static bool PublicInstancePropertiesEqual<t>(this T self, T to, params string[] ignore) where T : class
        {
            if (self != null && to != null)
            {
                var type = typeof(T);
                var ignoreList = new List<string>(ignore);
                
                var typeProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

                foreach (var pi in typeProperties)
                {
                    var property = type.GetProperty(pi.Name);

                    var selfValue = property.GetValue(self);
                    var toValue = property.GetValue(to);

                    if (selfValue != toValue)
                        return false;
                }

                return true;
            }

            return false;
        }</string></t>

Also, you've written an extension so you don't need to specify the Compare.... to use it. All you need to do is:
bool result = x.PublicInstancePropertiesEqual(y, ignoreList);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900