I have two classes and their variables are exactly the same.(To avoid prolonging the question, I don't explain why I'm using the same class.) I am comparing the same variables in these two classes with Reflection. The function returns true if there is a variable whose value has changed.. However, this function I wrote does not make the comparison correctly if there is a list in the class. Returns true for identical and unchanged lists, but the result should be false
Assume the list is full and the same values
CLASS ONE | CLASS TWO |
---|---|
ID = 5 | ID = 5 |
Number = 10 | Number = 10 |
List a | List a |
private bool Compare(Class A, Class B)
{
A.GetType().GetProperties().ToList().ForEach(p =>
{
B.GetType().GetProperties().ToList().ForEach(p2 =>
{
if (p.Name == p2.Name)
{
if
(!p.GetValue(A).Equals(p2.GetValue(B)))
{
result = true;
}
}
}
);
});
return result;
}