none
How can I enumerate the properties of an object in the order they are declared or any other known order? RRS feed

  • Question

  • Hello everybody.  I pretty much finished creating my first web application using a middle tier for the core components.  I have never done middle tiers in the past, so it is a learning process.

    But that is not the issue, and the issue is not ASP.net either.

    The problem at hand is:  I am data binding ASP.net controls like the repeater to a series of collections.  It worked nicely, so I got the hang of it.  Now I want to do this all the time.  I was so excited about this, that I needed to create a table that would display some (but not all) properties of a couple of objects from a couple of my classes.  Having a good understanding of IEnumerable and collections, and having a good understanding of property descriptors I decided to create a class that can receive an object of any type (type Object ) and can enumerate all properties that have BrowsableAttribute(true) as a series of items in a "collection".

    So the process was simple enough:  Create the new class and implement IEnumerable .  On construction it queries the passed object for all properties with the BrowsableAttribute attribute by using TypeDescriptor.GetProperties(new Attribute[] { new BrowsableAttribute(true) }) .  Then the enumerator object would iterate through this returned collection and presto!  My table generated consistently for any type of object that I wished.

    I did notice that the order the properties were enumerated was the order they were declared in the class.  I thought of this as VERY convenient.

    Today I published a RELEASE version (as opposed to the debug version I have been using in the past days), and now my tables show a different order for the properties!  The release build does not maintain the order of the declared properties.

    How can I reliably obtain a PropertyDescriptorCollection whose elements are ordered in a known way?  I don't really care if I can't get the order as in Debug, I just need a simple method for ordering the properties that is independent of the build.

    If there is no way in .Net, I was thinking of creating my own DisplayOrderAttribute attribute class and make the object that enumerates the properties ensure they are enumerated in the order set by the DisplayOrderAttribute attribute.  But if you have other suggestions, please feel free to post.

    Thanks!
    MCP
    Saturday, November 14, 2009 12:04 AM

Answers

  • I think your going to need to use your DisplayOrderAttribute.  I'm fairly certain that .NET makes no guarentee's regarding the order that reflection will return the property descriptors in.  I believe that it is usually vtable order, but the JIT compiler is free to optimize this how it sees fit.  I seem to remember people having code that relied on this order break between 1.1 and 2.0 because the compiler was more aggressive about optimizing the code...



    Tom Shelton
    • Marked as answer by webJose Monday, November 16, 2009 2:08 PM
    Saturday, November 14, 2009 1:01 AM

All replies

  • I think your going to need to use your DisplayOrderAttribute.  I'm fairly certain that .NET makes no guarentee's regarding the order that reflection will return the property descriptors in.  I believe that it is usually vtable order, but the JIT compiler is free to optimize this how it sees fit.  I seem to remember people having code that relied on this order break between 1.1 and 2.0 because the compiler was more aggressive about optimizing the code...



    Tom Shelton
    • Marked as answer by webJose Monday, November 16, 2009 2:08 PM
    Saturday, November 14, 2009 1:01 AM
  • Hello

    I second Tom. According to the documentation http://msdn.microsoft.com/en-us/library/ybh0y4fd.aspx, the order of the returned collection is not guaranteed to be identical between calls, so always order it before use. Please use your own DisplayOrder attribute.

    If you have any other questions, please feel free to post here.

     


    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, November 16, 2009 1:38 PM
    Moderator