In the .NET environment, Reflection provides several methods to obtain information about any type from the type system. One of these methods is GetProperties
method which retrieves a list of all the properties of a given type. This method returns an array of PropertyInfo
objects.
PropertyInfo[] propListInfo = type.GetProperties();
In most cases, you don't care, but the order of the properties does not have to be the same if you run this method several times. This is well described in the documentation of this method. Microsoft also states that your code should not be depending on the order of the properties obtained.
I had a very nice example of a bug resulting from the misuse of this method. An ObjectComparer
class, which is dedicated to the comparison of two objects of the same type by recursively comparing its properties, which I have inherited as legacy code on my current Silverlight project.
I have noticed that the results of the comparison are not the same everytime I run the comparison. Concretely, the first time the comparison was run on two same objects, it always told me that the objects are not equal. Take a look at the problematic code, which I have simplified a bit for this post:
private static bool CompareObjects(object initialObj, object currentObj, IList<String> filter)
{
string returnMessage = string.Empty;
Type type = initialObj.GetType();
Type type2 = currentObj.GetType();
PropertyInfo[] propListInfo = type.GetProperties(BindingFlags.GetProperty |
BindingFlags.Public | BindingFlags.Instance).Where(x => !filter.Contains(x.Name)).ToArray();
PropertyInfo[] propListInfo1 = type2.GetProperties(BindingFlags.GetProperty |
BindingFlags.Public | BindingFlags.Instance).Where(x => !filter.Contains(x.Name)).ToArray();
if (type.IsSealed == true && type.IsGenericType == false)
{
if (!initialObj.Equals(currentObj))
{
return false;
}
}
else
{
for (int count = 0; count < propListInfo.Length; count++){
var result = CompareValues(propListInfo[count].GetValue(initialObj),
propListInfo2[count].GetValue(currentObj));
if(result == false) {
return result;
}
}
}
return true;
}
So in order to correct this code, you will have to order both arrays by MetadataToken, which is a unique identifier of each property.
propListInfo = propListInfo.OrderBy(x=>x.MetadataToken).ToArray();
propListInfo1 = propListInfo1.OrderBy(x=>x.MetadataToken).ToArray();
There is some more information about how reflection works in this blog post. The issue is that the Reflection engine holds a "cache" for each type, in which it stocks the already "discovered" properties. The problem is that, this cache is cleared during garbage collection. When we ask for the properties, then they are served from the cache in the order in which they have been discovered.
However in my case, this information does not help. The issue occurs only the first time that I ask the ObjectComparator
to compare the objects and there is no reason that there should be any garbage collection between the first and second run... well no idea here. Sorting by MetadataToken has fixed the issue for me.
CodeProject