I’ve encountered a weird issue while using Reflection.
So I have a domain class with different properties. Depending on the needs, some properties are filled and some are not.
In the GUI, I iterate through the properties via GetProperties() and display them according to their value. (Empty not shown, not empty is shown).
But! The first time I iterate the properties, the order of the properties is different then the next times.
So the first time I get “ObjectMetaClassName”=>”ShortName”=>”Name”=>”Url”.
Subsequent times I get “ObjectMetaClassName”=>”ShortName”=>”Url”=>”Name”.
Does anyone have an explanation for this, why this occurs?
From http://msdn.microsoft.com/en-us/library/kyaxdd3x.aspx:
If order is important, perhaps you should try sorting the results?
Update Custom sorting
This will add some complexity, but you could add custom sort order to the results using attributes. First, create the custom attribute
SortOrderAttribute:Next apply that attribute to the properties on your class:
Then an IComparer:
Finally, to get the sorted list: