匿名联合和嵌套的struct转换为c#

时间:2013-02-04 12:17:15

标签: c# struct unions

我正在尝试在C#中创建下面的结构,以便我可以与我的Lumenera CCD进行通信。下面的代码取自c ++头文件。主要问题是匿名联合和嵌套结构部分。任何帮助你将不胜感激。一个等效的例子很棒。

typedef struct {
        FLOAT exposure;        
        FLOAT gain;            
        union {
         struct {
          FLOAT gainRed;     
          FLOAT gainBlue;     
          FLOAT gainGrn1;     
          FLOAT gainGrn2;     
         };
         struct {
          FLOAT gainMag;     
          FLOAT gainCyan;     
          FLOAT gainYel1;     
          FLOAT gainYel2;    
         };
        };
      union
      {
           BOOL useStrobe;        
         ULONG strobeFlags;   
      };

} LUCAM_SNAPSHOT;

1 个答案:

答案 0 :(得分:1)

[StructLayout(LayoutKind.Explicit)] 
public struct LUCAM_SNAPSHOT
{
    [FieldOffset(0)] public float exposure;
    [FieldOffset(4)] public float gain;
    [FieldOffset(8)] public float gainRed;
    [FieldOffset(8)] public float gainGrn1;
    [FieldOffset(8)] public float gainGrn2;
    [FieldOffset(12)] public float gainMag;
    [FieldOffset(12)] public float gainGrn1;
    [FieldOffset(12)] public float gainGrn2;
    [FieldOffset(12)] public float gainMag;
    [FieldOffset(16)] public byte useStrobe;
    [FieldOffset(16)] public int strobeFlags;
}

我认为它可能与上述类似。我还没试过,但至少你有一个起点。