如果GetFields()不能保证秩序,请问LayoutKind.Sequential工作 [英] if GetFields() doesn't guarantee order, how does LayoutKind.Sequential work
问题描述
我需要得到字段信息在保证的顺序相对于声明顺序。现在,我使用属性来指定顺序。
I need to get fieldinfo in a guaranteed order with respect to declaration order. Right now I'm using attributes to specify order.
有没有这个做什么?
有没有人对如何 LayoutKind.Sequential
工作知识,如果我能够把它的技术。
Does anyone have knowledge of how LayoutKind.Sequential
works, and if I can apply its technique.
我不T看看 LayoutKind.Sequential
的作品,除非有一些预编译的代码,增加了属性。
I don't see how LayoutKind.Sequential
works, unless there's some precompiler code that adds attributes.
推荐答案
如果您想通过 Type.GetFields
的排序是稳定的,尝试的的 MetadataToken
属性。
If you want the ordering of the fields returned by Type.GetFields
to be stable, try sorting by the MetadataToken
property.
Type myType = ...
BindingFlags flags = ...
IEnumerable<FieldInfo> orderedFields = myType.GetFields(flags)
.OrderBy(field => field.MetadataToken);
根据经验,以这种方式订购领域已被发现的在声明顺序回报他们,虽然这不是记录
通过顺便说一下,因为要求并不完全意义的问题;没有任何理由相信反射API以任何方式对运行时如何布置内存中的对象进行捆绑。
By the way, the question as asked doesn't entirely make sense; there isn't any reason to believe that the reflection API is tied in any way to how the runtime lays objects out in memory.
这篇关于如果GetFields()不能保证秩序,请问LayoutKind.Sequential工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!