Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Type.GetProperties() guarantee a certain order for the PropertyInfo[] result?

Tags:

c#

reflection

Does Type.GetProperties() guarantee a certain order for its PropertyInfo[] result? Such as returning them in alphabetical order by property name or the order they appear in code. Or is the order undefined?

like image 333
Jonathon Watney Avatar asked Oct 06 '09 20:10

Jonathon Watney


People also ask

What is GetProperties C#?

GetProperties() Returns all the public properties of the current Type.

How do I get a property Type from PropertyInfo?

You can do this by getting an array of all properties from the Type. GetProperties method and then iterating the elements in the array, or you can retrieve the PropertyInfo object that represents the property directly by calling the Type. GetProperty method and specifying the property name.

What is GetType GetProperty in c#?

GetProperties() Method is used to get the properties of the current Type.


2 Answers

From MSDN:

The GetProperties method does not return properties in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which properties are returned, because that order varies.

like image 124
Martin Harris Avatar answered Oct 03 '22 16:10

Martin Harris


I think you can sort the array again using "PropertyInfo.MetadataToken" Like this :

Array.Sort(propertyInfos, delegate(PropertyInfo first, PropertyInfo second)
  {
            return first.MetadataToken.CompareTo(second.MetadataToken);
  });
like image 28
user1069711 Avatar answered Oct 03 '22 14:10

user1069711