Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetFields in declaration order #19732

Closed
tmds opened this issue Dec 17, 2016 · 3 comments
Closed

GetFields in declaration order #19732

tmds opened this issue Dec 17, 2016 · 3 comments
Labels
area-System.Reflection documentation Documentation bug or enhancement, does not impact product or test code
Milestone

Comments

@tmds
Copy link
Member

tmds commented Dec 17, 2016

If you search the internet how to do this, you will find the advice to sort by MetadataToken (e.g.: http://stackoverflow.com/questions/8067493/if-getfields-doesnt-guarantee-order-how-does-layoutkind-sequential-work)

Is this the proper way to do this? If so, perhaps it can be included in the documentation.
Does this need LayoutKind.Sequential to work?

@karelz
Copy link
Member

karelz commented Dec 19, 2016

I am pretty sure that TypeLoader uses metadata order for LayoutKind.Sequential.

What are the scenarios when you need the right order in Reflection? Or is it just curiosity question?
If the scenario is rare (the StackOverflow question does not have that many hits), I would suggest to not pollute the docs with deep dive details like this. Whoever needs it, can search internet. If it is in main docs, it might lead devs to use MetadataToken more and depend on it where they should not.

Anyway, if you still think some docs should be enhanced, please post link to the docs and suggest the changes (quote the text before and after).

@tmds
Copy link
Member Author

tmds commented Dec 20, 2016

I am pretty sure that TypeLoader uses metadata order for LayoutKind.Sequential.

Since no one complained on the SO, I also believe it will work. Would be great if we can be 100% sure. Also, can someone confirm 'LayoutKind.Sequential' is required for this to work.

What are the scenarios when you need the right order in Reflection? Or is it just curiosity question?
If the scenario is rare (the StackOverflow question does not have that many hits), I would suggest to not pollute the docs with deep dive details like this. Whoever needs it, can search internet. If it is in main docs, it might lead devs to use MetadataToken more and depend on it where they should not.

My use case is serialization. I agree, if the use case is not common enough it shouldn't clutter the docs.

Anyway, if you still think some docs should be enhanced, please post link to the docs and suggest the changes (quote the text before and after).

https://msdn.microsoft.com/en-us/library/ch9714z3(v=vs.110).aspx

The documentation has a paragraph on the order:

The GetFields method does not return fields in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which fields are returned, because that order varies.

We can add a line there:

To get the fields in declaration order [of layout sequential types], you can sort by the MetadataToken property.

@karelz
Copy link
Member

karelz commented Dec 20, 2016

Also, can someone confirm 'LayoutKind.Sequential' is required for this to work.

That's correct. For non-sequential, the runtime is free to rearrange fields as it feels fit.

I agree, if the use case is not common enough it shouldn't clutter the docs.

Agree. Let's not clutter the docs then. If the SO topics gets lots of hits, we can reconsider later.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Reflection documentation Documentation bug or enhancement, does not impact product or test code
Projects
None yet
Development

No branches or pull requests

3 participants