c# - Properties in a particular order -


using reflection have tool gets properties of class:

foreach (memberinfo member in typeof(t).getproperties(bindingflags.instance | bindingflags.public)) {     writevalue(streamwriter, member.name); } 

is there way ask "getproperties" return memberinfo's in order defined in class. doubt it, thought i'd ask.

class person {      public int id { get; set; }      public int age { get; set; } } 

i'd memberinfo's in order then: id, age

[caution: use @ own discresion these microsoft's impl details, may change in future releases]

update: mono seems work too

i've observed consitent behaviour using ms compilers since v3.5 when stumbled upon this:

using system; using system.linq; using system.reflection;   namespace consoleapplication1 {     class program     {         static void main(string[] args)         {                 typeof(test).getmembers(bindingflags.public | bindingflags.instance | bindingflags.declaredonly)                 .orderby(member => member.metadatatoken).tolist()                 .foreach(member => console.writeline(member.name));              console.readline();         }     }      public class test     {         public int secondproperty { get; set; }         public int firstproperty { get; set; }      } } 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -