If the property type is a value type (for example, int), the value returned by GetValue will be placed in the object. Then == will compare the links of these values ββin the box. If you want to have the correct comparison, you should use the Equals method:
class Program { class Test { public int A { get; set; } } static void Main(string[] args) { var testA = new Test { A = 1 }; var testB = new Test { A = 1 }; var propertyInfo = typeof(Test).GetProperties().Where(p => p.Name == "A").Single(); var valueA = propertyInfo.GetValue(testA, null); var valueB = propertyInfo.GetValue(testB, null); var result = valueA == valueB;
Johann Blais
source share