Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The short answer is no, there doesn't seem to be an easy way of getting C++ structs into a C# format. There is, however, a way of doing unions in C# structs; whether this is a good idea, or if it's better to start from scratch with classes, will depend on you.</p> <p>To get unions in C# structs you use <code>LayoutKind.Explicit</code> and the <code>FieldOffset</code> attribute:</p> <pre><code>[StructLayout(LayoutKind.Explicit)] struct StructTest { // A byte at the beginning of the struct. [FieldOffset(0)] public byte byte1; // Another byte immediately after it. [FieldOffset(1)] public byte byte2; // Now an integer which covers both. [FieldOffset(0)] public Int16 int1; } </code></pre> <p>You can use this how you would expect:</p> <pre><code> var foo = new StructTest(); foo.byte1 = 3; foo.byte2 = 6; Console.WriteLine("{0}", foo.int1); var bar = new StructTest(); bar.int1 = 5050; Console.WriteLine("{0}, {1}", bar.byte1, bar.byte2); </code></pre> <p>produces <code>1539</code> and <code>186, 19</code>.</p> <p>This gets you the union structure, but it still doesn't answer the main problem of BitFields. There doesn't seem to be a consensus on this (except, of course, what you've already done; see, for example, [<a href="https://stackoverflow.com/a/9311624/920242">1</a>]). It might be possible to do something clever with the <code>MarshalAs</code> attribute to map a custom BitField class onto the underlying struct, but at that level of effort it might be easier to keep writing custom classes as you have been.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload