.NET Reflections

Kntx

Platinum Member
Dec 11, 2000
2,270
0
71
If I have 2 objects of the same class, and I do one of these...

string[] props1 = object1.GetType().GetProperties();
string[] props2 = object2.GetType().GetProperties();

Are the entries of the props1 and props2 arrays guaranteed to be in the same order???
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
First of all, GetProperties() returns an array of PropertyInfo[] objects and not strings. The PropertyInfo object has a Name property which you can use to get the name of the property. I don't think that there is any guarantee that the properties will be ordered, but in practice they probably will be. If I were you though, I wouldn't rely on them being ordered. If you think that you have to rely on them being ordered, post a little more about the problem you are trying to solve and we'll see if we can help.
 

Kntx

Platinum Member
Dec 11, 2000
2,270
0
71
Yea, it does return propertyinfos a slip up on my part.

It doesn't really matter that they are in the same order. It is just easier to loop thru the two arrays simultaneously to compare/assign or whatever rather than having to find the corresponding object in the other array.

Anyways, now that I'm coming back to this I can't quite remember what I was trying to do in the first place. I don't even think I need 2 arrays.

Thanx for the response!
 

torpid

Lifer
Sep 14, 2003
11,631
11
76
Actually I believe it would by return them in the same order, guaranteed, IF the underlying type were the same. If object1 and object2 are different types, all bets are off. If they are the same type, then the GetType() method should point to a singular system.type instance from both calls. It would be absurd if every object had its own underlying instance of system.type.
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
Originally posted by: torpid
Actually I believe it would by return them in the same order, guaranteed, IF the underlying type were the same. If object1 and object2 are different types, all bets are off. If they are the same type, then the GetType() method should point to a singular system.type instance from both calls. It would be absurd if every object had its own underlying instance of system.type.


Yep, you're right. The underlying type does act as a singleton. This can be verified by looking at the GUID property of the Type object returned by GetType(). So in that case, it is guaranteed that you will get the same order of properties. So that just means that calling GetProperties() twice on the same type and looping through each of the PropertyInfo arrays is even more pointless than when we started. :)

Good catch.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Are you simply trying to clone an object? It certainly looks like it. Have you looked at System.Object.MemberwiseClone? A shallow copy is likely all you need, but if you wish for more you should at least implement ICloneable to indicate that your objects support such behavior.
 

Kntx

Platinum Member
Dec 11, 2000
2,270
0
71
Originally posted by: Descartes
Are you simply trying to clone an object? It certainly looks like it. Have you looked at System.Object.MemberwiseClone? A shallow copy is likely all you need, but if you wish for more you should at least implement ICloneable to indicate that your objects support such behavior.

Yes, but not exactly. I am trying to clone an object, but not all of the properties should be copied over. Only those properties that have been modified by the user should be copied to the new object (i.e. properties that have been changed since the object was constructed).
 

torpid

Lifer
Sep 14, 2003
11,631
11
76
Originally posted by: Kntx
Originally posted by: Descartes
Are you simply trying to clone an object? It certainly looks like it. Have you looked at System.Object.MemberwiseClone? A shallow copy is likely all you need, but if you wish for more you should at least implement ICloneable to indicate that your objects support such behavior.

Yes, but not exactly. I am trying to clone an object, but not all of the properties should be copied over. Only those properties that have been modified by the user should be copied to the new object (i.e. properties that have been changed since the object was constructed).

Sounds like you are implementing some form of object relational mapping? I would suggest that you make the copy initially and then the user only works with the copy and never the original.