Order of custom attributes (declared<-->runtime)

  • Thread starter Thread starter Chango V.
  • Start date Start date
C

Chango V.

Hello,

I have multiple instances of a custom attribute class applied to a field.
The problem is Type.GetCustomAttributes() returns them in a different order
than declared. The documentation seems to say that there is no defined order
for custom attributes, but it's really important for me to get them in the
declared order. What's funny is that if I try to rearrange them and
recompile, I will be able to see them at runtime just in the declared order.
But after I recompile once or twice more, without changing the attribute
declarations (maybe only something else in the code), the order gets
randomized! And then this seemingly random order persists.

Is there some particular implementation detail about custom attributes that
could help me figure out what's going on and, hopefully, solve my problem?
Any hint will be appreciated.

Thanks.

//
 
Chango,

The order that the attributes are returned is undefined and not
guaranteed to be consistent. Because of this, you should have a secondary
property on your attribute that sets the order, and then you can perform the
sorting on the attributes yourself.

Hope this helps.
 
Hi Chango,

Do you still have any concern on this issue?

After doing a lot of research, I think there is not a good way to get the
attributes collection order. I think Nicholas's suggestion of using another
property to explicit contain the attributes order may meet your
need(Although it breaks your elegance).

If you feel un-confortable about this design, you may provide your
suggestion to:
http://register.microsoft.com/mswish/suggestion.asp
or mail to: (e-mail address removed)

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks for the response, Jeffrey. Apparently there has been some fundamental design decision there, so I don't believe there is much chance for change. But at least I'm assured I'm not missing an easy possible solution. And, in general, adding an extra property for explicit ordering may be a reasonable price for achieving the goal

Regards

// Chang
 
Hi Chango,

Thanks very much for your feedback.

Also thanks for your understanding. If you have any further concern, please
feel free to post, I will help you .

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top