I am doing encoding and decoding using reflection.
when I read the this.GetType().Getfields(), in some machines it is returned in a random order, I need to order them back to do the encoding and decoding correctly.
Can I sort the array returned from this.GetType().GetFields() using FieldInfo.FieldHandle
Like this :
FieldInfo[] infos = GetType().GetFields();
Array.Sort(infos, delegate(FieldInfo first, FieldInfo second)
{
return first.FieldHandle.Value.ToInt32().CompareTo(second..Value.ToInt32());
});
is it the correct way.
I mean Now it is give me correct ordering, but i don’t know if it will do every time I call it(timing or time of processing issues).
is there any problem may happen in the future from this code.
my question is about using “FieldHandle” in ordering.
if it is not stable what can I use instead of it.
Regards,
A much simpler way would be to use Linq:
But remember that the FieldHandle isn’t necessarily a stable or meaningful field to sort by.
EDIT: I see you’re asking about using FieldHandle to sort by, in general. In that case, the answer is YES, you WILL have problems with it in the future. The FieldHandle is a pointer to a structure in memory. This memory location is RANDOMLY allocated. Every time you run your program, you’ll get a different set of FieldHandles, and your sorting will be different. This isn’t a unique and persistent ID of the field, it’s just a memory pointer.
What are you trying to accomplish with this sorting? Maybe we can suggest some alternatives.