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

后端 未结 3 1554
孤独总比滥情好
孤独总比滥情好 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

    LayoutKind.Sequential specifies that the fields of the type should be laid out in memory in the same order they are declared in your source code.

    Without the attribute the CLR is free to optimize memory use by rearranging the fields.

    So the attribute just adds metadata that tells the CLR not to do any in memory optimalisation that messes up the order of fields.

提交回复
热议问题