I have two class :
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public bool isActif { get; set; }
public Product[] Product { get; set; }
}
public class Product
{
public string Id { get; set; }
}
And two instances :
Customer Customer1 = new Customer
{
FirstName = "FirstName1",
LastName = "LastName1",
isActif = true,
Product = new Product[]
{
new Product()
{
Id = "1"
}
}
};
Customer Customer2 = new Customer
{
FirstName = "FirstName2",
LastName = "LastName2",
isActif = false,
Product = new Product[]
{
new Product()
{
Id = "2"
}
}
};
I have one method who compare all properties of the two instances :
public static bool AreEquals(object objectA, object objectB)
{
if (objectA == null && objectB == null)
return true;
if (objectA != null && objectB != null)
{
Type objectType = objectA.GetType();
foreach (PropertyInfo propertyInfo in
objectType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(x => x.CanRead))
{
object valueA = propertyInfo.GetValue(objectA, null);
object valueB = propertyInfo.GetValue(objectB, null);
if (typeof(IComparable).IsAssignableFrom(propertyInfo.PropertyType) ||
propertyInfo.PropertyType.IsPrimitive ||
propertyInfo.PropertyType.IsValueType)
{
if (!AreValuesEqual(valueA, valueB))
Console.WriteLine(propertyInfo.ReflectedType.Name + "." + propertyInfo.Name + " not equal");
}
else if (!AreEquals(valueA, valueB))
return false;
}
return true;
}
return false;
}
But when I get to the property Product
, I have an StackOverflowException
generated. Why ? And how to loop the property if it is a array ?
Aucun commentaire:
Enregistrer un commentaire