General comparison of properties of objects with small recursion for loading

I have several complex objects that I would like to compare properties against. The following code does an excellent job until you get into the collection. I would like to recursively call a function with each member of the collection. Can someone take a look and help me determine the type of object in the collection so that I can call HasPropertyChanged again?

This pseudo code shows my intention

      if (p.GetType() == typeof(System.Collections.Generic.List<>))
       {
            foreach (var blah in //the list)
            {
                HasPropertyChanged<//TheType>(Type obj1, Type obj2, null);
            }
        }

Also, this part of the code scares me. If I do not call the tostring method, I will get some funky results, for example, id 63633, not equal to 63633

    object val1 = Original.GetType().GetProperty(p.Name).GetValue(Original, null);
        object val2 = Modified.GetType().GetProperty(p.Name).GetValue(Modified, null);
        if (!IgnoreProperties.Contains(p.Name) && 
            val1 != null && val2 != null && 
            val1.ToString() != val2.ToString())
        {
            return true;
        }

Here it is in its entirety.

private bool HasPropertyChanged<T>(T Original, T Modified, string[] IgnoreProperties)
    {
        if (Original == null || Modified == null)
            return false;
        if (IgnoreProperties == null)
            IgnoreProperties = new string[] { };

        IEnumerable<PropertyInfo> properties = typeof(T).GetProperties();

        foreach (var p in properties)
        {
            if (p.GetType() == typeof(System.Collections.Generic.List<>))
            {
                foreach (var blah in //the list)
                {
                    HasPropertyChanged<//TheType>(Type obj1, Type obj2, null);
                }
            }
            object val1 = Original.GetType().GetProperty(p.Name).GetValue(Original, null);
            object val2 = Modified.GetType().GetProperty(p.Name).GetValue(Modified, null);
            if (!IgnoreProperties.Contains(p.Name) && 
                val1 != null && val2 != null && 
                val1.ToString() != val2.ToString())
            {
                return true;
            }
        }
        return false;
    }
+3
source share
2 answers

, - , Type:

private bool HasPropertyChanged<T>(T Original, T Modified, string[] IgnoreProperties)
{
    return HasPropertyChanged(typeof(T), Original, Modified, IgnoreProperties);
}

private bool HasPropertyChanged(Type T, object Original, object Modified, string[] IgnoreProperties)
{
    // ...
}

:

if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(System.Collections.Generic.List<>))
{
    object val1 = p.GetValue(Original, null);
    object val2 = p.GetValue(Modified, null);

    // First check count...
    PropertyInfo countProperty = p.PropertyType.GetProperty("Count");
    int count1 = countProperty.GetValue(val1, null);
    int count2 = countProperty.GetValue(val2, null);
    if (count1 != count2) return true;

    // Now iterate:
    var enumerator1 = (val1 as System.Collections.IEnumerable).GetEnumerator();
    var enumerator2 = (val2 as System.Collections.IEnumerable).GetEnumerator();
    while (enumerator1.MoveNext())
    {
        enumerator2.MoveNext();
        // check for null, skipping here...
        object elem1 = enumerator1.Current;
        object elem2 = enumerator2.Current;
        if (HasPropertyChanged(elem1.GetType(), elem1, elem2, IgnoreProperties)) return true;
    }
}
// ...
+1

-, ==, Equals(). ;

object val1 = Original.GetType().GetProperty(p.Name).GetValue(Original, null);
object val2 = Modified.GetType().GetProperty(p.Name).GetValue(Modified, null);

. true, , , val1 val2 - . .ToString() :

if (val1.Equals(val2) { ... }

, ( ).

- ;

if (p is IEnumerable)
{
    foreach (object o in p)
    {
        if (!o.HasPropertyChanged(...))
            return false;
    }
}

, . , , - , , , . , , T "" "" ;

if (Original.GetType() != Modified.GetType())
    return false;
+1

All Articles