Click here to Skip to main content
15,119,897 members
Articles / Programming Languages / C#
Technical Blog
Posted 3 Oct 2012

Tagged as


1 bookmarked

Reflection & Order of Discovered Properties

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
3 Oct 2012CPOL2 min read
Reflection & Order of Discovered Properties

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 class type is native i.e. string, int, boolean, etc.
 if (type.IsSealed == true && type.IsGenericType == false)
  if (!initialObj.Equals(currentObj))
   return false;
 else //class type is object
  //loop through each property of object
  for (int count = 0; count < propListInfo.Length; count++){
   var result = CompareValues(propListInfo[count].GetValue(initialObj),
   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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Junior) OCTO Technology
Czech Republic Czech Republic
Writing software at ITG RFQ-hub.
Articles at OCTO blog

Comments and Discussions

SuggestionI know it wasn't the point, but this could be more efficient Pin
Matt T Heffron3-Oct-12 14:41
professionalMatt T Heffron3-Oct-12 14:41 
GeneralRe: I know it wasn't the point, but this could be more efficient Pin
hoonzis4-Oct-12 11:45
Memberhoonzis4-Oct-12 11:45 

This looks greate. Is it a code which you are already using in production?

Anyway you are rihgt I should not be showing "ugly" code. I have picked up old legacy object compiler which is used quite extensively in a Silverlight project. We don't realy have performance issues (it is used to compare some client side DTO's), but I might rewrite the way you have shown, just to have clean code.

Can I make your code part of the article, just to show a better way to do the stuff?

GeneralRe: I know it wasn't the point, but this could be more efficient Pin
Matt T Heffron4-Oct-12 12:24
professionalMatt T Heffron4-Oct-12 12:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.