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
{
HasPropertyChanged<
}
}
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
{
HasPropertyChanged<
}
}
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;
}
source
share