if GetFields() doesn't guarantee order, how does LayoutKind.Sequential work

后端 未结 3 1553
孤独总比滥情好
孤独总比滥情好 2020-12-14 17:32

I need to get fieldinfo in a guaranteed order with respect to declaration order. Right now I\'m using attributes to specify order.

Is there a more automatic way of d

3条回答
  •  一个人的身影
    2020-12-14 18:20

    The question is old but not so old... I'm dealing now with the same problem. And I prefer to get the fields in tho order of declaration. The following call should work for a value type or a formatted reference type.

    var fields = type.GetFields().OrderBy(f => Marshal.OffsetOf(type, f.Name).ToInt32());
    

    Enjoy!

提交回复
热议问题