C++ unions can be matched in .NET by the layouts created using
[FieldOffset]
member attribute,
System.Runtime.InteropServices.FieldOffsetAttribute
. Please see:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.fieldoffsetattribute.aspx[
^].
The code sample in the MDSN article referenced above clearly shows how to use the attribute, but perhaps you need an idea on how to mimic the union. Here is how: in case of union, members participating in different cases of union take identical addresses relative to the starting of the structure. Hence, with
[FieldOffset]
, you will have to specify the same offset to two different members representing members of the union. In your case, the members
A
and
B
should be at the same offset, which you can calculate taking into consideration member layout of the structure (aligned by 32-bit, 16-bit or 8-bit boundary) and memory taken by the previous members in sequential order. I would advice to use this attribute on all previous members of the structure, for certainty.
—SA