|
| 1 | +using ObjectLayoutInspector; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | + |
| 4 | +namespace StructTest; |
| 5 | + |
| 6 | +internal static class StructTest |
| 7 | +{ |
| 8 | + |
| 9 | + #region Constants & Statics |
| 10 | + |
| 11 | + public static void Test() |
| 12 | + { |
| 13 | + var box = new BoxStruct(false, 0, new InnerStruct(100, true)); |
| 14 | + |
| 15 | + TypeLayout.PrintLayout(box.GetType(), true); |
| 16 | + |
| 17 | + // LayoutKind.Auto |
| 18 | + //Type layout for 'BoxStruct' |
| 19 | + //Size: 16 bytes. Paddings: 5 bytes (%31 of empty space) |
| 20 | + //|=========================================| |
| 21 | + //| 0-3: Int32 _intField (4 bytes) | |
| 22 | + //|-----------------------------------------| |
| 23 | + //| 4: Boolean _boolField (1 byte) | |
| 24 | + //|-----------------------------------------| |
| 25 | + //| 5-7: padding (3 bytes) | |
| 26 | + //|-----------------------------------------| |
| 27 | + //| 8-15: InnerStruct _inner (8 bytes) | |
| 28 | + //| |=====================================| | |
| 29 | + //| | 0-3: Int32 _intField (4 bytes) | | |
| 30 | + //| |-------------------------------------| | |
| 31 | + //| | 4: Boolean _boolField2 (1 byte) | | |
| 32 | + //| |-------------------------------------| | |
| 33 | + //| | 5: Boolean _boolField (1 byte) | | |
| 34 | + //| |-------------------------------------| | |
| 35 | + //| | 6-7: padding (2 bytes) | | |
| 36 | + //| |=====================================| | |
| 37 | + //|=========================================| |
| 38 | + |
| 39 | + //LayoutKind.Sequential |
| 40 | + //Type layout for 'BoxStruct' |
| 41 | + //Size: 20 bytes. Paddings: 9 bytes (%45 of empty space) |
| 42 | + //|=========================================| |
| 43 | + //| 0: Boolean _boolField (1 byte) | |
| 44 | + //|-----------------------------------------| |
| 45 | + //| 1-3: padding (3 bytes) | |
| 46 | + //|-----------------------------------------| |
| 47 | + //| 4-7: Int32 _intField (4 bytes) | |
| 48 | + //|-----------------------------------------| |
| 49 | + //| 8-19: InnerStruct _inner (12 bytes) | |
| 50 | + //| |=====================================| | |
| 51 | + //| | 0: Boolean _boolField2 (1 byte) | | |
| 52 | + //| |-------------------------------------| | |
| 53 | + //| | 1-3: padding (3 bytes) | | |
| 54 | + //| |-------------------------------------| | |
| 55 | + //| | 4-7: Int32 _intField (4 bytes) | | |
| 56 | + //| |-------------------------------------| | |
| 57 | + //| | 8: Boolean _boolField (1 byte) | | |
| 58 | + //| |-------------------------------------| | |
| 59 | + //| | 9-11: padding (3 bytes) | | |
| 60 | + //| |=====================================| | |
| 61 | + //|=========================================| |
| 62 | + } |
| 63 | + |
| 64 | + #endregion |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | +[StructLayout(LayoutKind.Sequential)] |
| 69 | +public readonly record struct BoxStruct |
| 70 | +{ |
| 71 | + public readonly bool _boolField; |
| 72 | + public readonly int _intField; |
| 73 | + public readonly InnerStruct _inner; |
| 74 | + |
| 75 | + public BoxStruct(bool boolField, int intField, InnerStruct inner) |
| 76 | + { |
| 77 | + _boolField = boolField; |
| 78 | + _intField = intField; |
| 79 | + _inner = inner; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +[StructLayout(LayoutKind.Sequential)] |
| 84 | +public readonly record struct InnerStruct |
| 85 | +{ |
| 86 | + public readonly bool _boolField2; |
| 87 | + public readonly int _intField; |
| 88 | + public readonly bool _boolField; |
| 89 | + |
| 90 | + public InnerStruct(int intField, bool boolField) |
| 91 | + { |
| 92 | + _intField = intField; |
| 93 | + _boolField = boolField; |
| 94 | + } |
| 95 | +} |
0 commit comments