Convert call is not needed. The values returned by GetValue are already int s. Simple casting gives the right result.
private class Test { public int y; } private static void Main() { Test test1 = new Test { y = 1 }; Test test2 = new Test { y = 1 }; FieldInfo[] fields = test1.GetType().GetFields(); int test1Value = (int)fields[0].GetValue(test1); int test2Value = (int)fields[0].GetValue(test2); Console.WriteLine(test1Value);
The reason non-locked values fail is because Convert.ChangeType still returns an object , which is an attachment , so that you really get the reference equality.
Another way to get the correct value is to call the Equals method, which will be correctly redirected to Int32.Equals and print true :
Console.WriteLine(test1Converted.Equals(test2Converted)); // prints true
Please note that in this case Convert.ChangeType is still not required.
source share