Type.GetProperties() question

  • Thread starter Thread starter Patrick
  • Start date Start date
P

Patrick

Hello All,
Does anyone know what order GetProperties() returns public properties in?
I'm using custom Attributes on the properties of my Business objects
(NHibernate) to let me know if they should be displayed in a grid or not. I
would like them to display in the order in which I've defined them in the
class, but GetProperties() seems to return the properties "out of whack"
from how I've coded them. Is there any kind of ordinal property or something
to that effect?
Any help is greatly appreciated
Sincerely,
Patrick
 
Patrick said:
Hello All,
Does anyone know what order GetProperties() returns public properties in?
I'm using custom Attributes on the properties of my Business objects
(NHibernate) to let me know if they should be displayed in a grid or not. I
would like them to display in the order in which I've defined them in the
class, but GetProperties() seems to return the properties "out of whack"
from how I've coded them. Is there any kind of ordinal property or something
to that effect?
Any help is greatly appreciated
Sincerely,
Patrick

Patrick,

..NET does not keep these things in any particular order. The only way
to do this is to add a parameter to your attribute that tells it's
order.
 
Ok, I'm dumb...it was as problem when loading into the grid, not with
reflection. Appearently GetProperties does return them in the order in wich
they were coded.
 
Thanks Tom for the quick reply. (I love the professionals :) I can
certainly do that, although I'd hate to have to maintain hardcoded indexes
when properties are added or deleted! But since I don't have a choice....
(Listen up MS programmers! I want reflected properties to come back in the
order they were coded! heh heh)
Best wishes,
Patrick
 
Patrick said:
Ok, I'm dumb...it was as problem when loading into the grid, not with
reflection. Appearently GetProperties does return them in the order in wich
they were coded.

Patrick - I maybe the one who is dumb here :) But, I'm pretty sure I
have a case where GetProperties indeed does not return the properties
in the order of declaration. Thinking about this, it would be the
natural assumption that it would return the properties in vtable order,
and that the vtable would be in the order of declaration - but, I'm not
100% sure that is really the case or is guaranteed. I am going to look
at that code tomorrow, and maybe do a little more research here.
 
Patrick said:
Thanks Tom for the quick reply. (I love the professionals :) I can
certainly do that, although I'd hate to have to maintain hardcoded indexes
when properties are added or deleted! But since I don't have a choice....
(Listen up MS programmers! I want reflected properties to come back in the
order they were coded! heh heh)

With partial classes, the very concept of "the order they were coded"
doesn't even exist any more...
 
Tom Shelton said:
Patrick - I maybe the one who is dumb here :) But, I'm pretty sure I
have a case where GetProperties indeed does not return the properties
in the order of declaration. Thinking about this, it would be the
natural assumption that it would return the properties in vtable order,
and that the vtable would be in the order of declaration - but, I'm not
100% sure that is really the case or is guaranteed. I am going to look
at that code tomorrow, and maybe do a little more research here.

I suspect it's much like running a SELECT query on a database without
specifying an 'ORDER BY' clause and hoping to get rows back in the same
order they were added. It *might* happen by chance - but technically
speaking the order is undefined.
 
Adam Clauss said:
I suspect it's much like running a SELECT query on a database without
specifying an 'ORDER BY' clause and hoping to get rows back in the same
order they were added. It *might* happen by chance - but technically
speaking the order is undefined.

Or at least, it's undefined *in general*, although some databases may
well define an order.
 
Jon said:
With partial classes, the very concept of "the order they were coded"
doesn't even exist any more...


Good point. I don't think there has ever been a guarentee on this
anyway.
 
Back
Top