Can I set the size attribute of StructLayout at runtime? <p>Im trying to use SendInput to simulate keyboard presses in my app and want to support both 32-bit and 64-bit.</p> <p>I've determined that for this to work, I need to have 2 different INPUT structs as such</p> <pre><code> [StructLayout(LayoutKind.Sequential)] public struct KEYBDINPUT { public ushort wVk; // Virtual Key Code public ushort wScan; // Scan Code public uint dwFlags; public uint time; public IntPtr dwExtraInfo; } [StructLayout(LayoutKind.Explicit, Size = 28)] public struct INPUT32 { [FieldOffset(0)] public uint type; // eg. INPUT_KEYBOARD [FieldOffset(4)] public KEYBDINPUT ki; } [StructLayout(LayoutKind.Explicit, Size = 40)] public struct INPUT64 { [FieldOffset(0)] public uint type; // eg. INPUT_KEYBOARD [FieldOffset(8)] public KEYBDINPUT ki; } </code></pre> <p>I wanted to know if there was a way to set the <code>StructLayout</code> size and <code>FieldOffsets</code> at runtime so I could use just one <code>INPUT</code> struct and determine the size and fieldoffset depending on the machine.</p> <p>I have tried the code below but I would like to know if the same is possible at runtime instead of compile time. </p> <pre><code>#if _M_IX86 [StructLayout(LayoutKind.Explicit, Size = 28)] #else [StructLayout(LayoutKind.Explicit, Size = 40)] #endif public struct INPUT { [FieldOffset(0)] public uint type; // eg. INPUT_KEYBOARD #if _M_IX86 [FieldOffset(4)] #else [FieldOffset(8)] #endif public KEYBDINPUT ki; } </code></pre>