错误:无法加载类型'test2',因为它包含偏移0处的对象字段,该字段未正确对齐或由非对象字段重叠

时间:2014-08-21 10:48:17

标签: c#

  [StructLayout(LayoutKind.Explicit)]
  //[StructLayout(LayoutKind.Sequential)]

  public struct test
    {

         [FieldOffset(0)]
          public byte itemcode;
         [FieldOffset(1)]
         public byte method;/*1*/
         [FieldOffset(2)]
         public byte wavelen_1;/*1*/
         [FieldOffset(3)]
         public byte wavelen_2;/*1*/
         [FieldOffset(4)]
         public byte units;/*1*/
         [FieldOffset(5)]
         public byte temp;/*1*/
         [FieldOffset(6)]
         public byte slope_reaction;
         [FieldOffset(7)]
         public byte blank;
         [FieldOffset(8)]
         public byte slope_units;//mar 3
         [FieldOffset(9)]
         public byte num_stds;
         [FieldOffset(10)]
         public byte incub_time;
         [FieldOffset(11)]
         public byte num_read;
         [FieldOffset(12)]
         public byte decimals;
         [FieldOffset(13)]
         public byte std_repeat;
         [FieldOffset(14)]
         public byte sample_repeat;
         [FieldOffset(15)]
         public byte dmax;
         [FieldOffset(16)]
         public ushort regvol_1;
         [FieldOffset(18)]
         public ushort regvol_2;
         [FieldOffset(20)]
         public ushort samp_vol;
         [FieldOffset(22)]
         public ushort asp_vol;
         [FieldOffset(24)]
         public ushort linear_limit;
         [FieldOffset(26)]
         public ushort normal_low;
         [FieldOffset(28)]
         public ushort normal_high;
         [FieldOffset(30)]
         public ushort abs_limit;
         [FieldOffset(32)]
         public ushort delta;
         [FieldOffset(34)]
         public ushort delay;//100bytes
         [FieldOffset(36)]
         public float abs_high;
         [FieldOffset(40)]
         public float abs_low;
         [FieldOffset(44)]
         public float factor;//90 bytes


         [FieldOffset(48)]
         public float std_abs0;
         [FieldOffset(52)]
         public float std_abs1;
         [FieldOffset(56)]
         public float std_abs2;
         [FieldOffset(60)]
         public float std_abs3;
         [FieldOffset(64)]
         public float std_abs4;
         [FieldOffset(68)]
         public float std_abs5;
         [FieldOffset(72)]
         public float std_abs6;
         [FieldOffset(76)]
         public float std_abs7;
         [FieldOffset(80)]
         public float std_abs8;
         [FieldOffset(84)]
         public float std_conc0;
         [FieldOffset(88)]
         public float std_conc1;
         [FieldOffset(92)]
         public float std_conc2;
         [FieldOffset(96)]
         public float std_conc3;
         [FieldOffset(100)]
         public float std_conc4;
         [FieldOffset(104)]
         public float std_conc5;
         [FieldOffset(108)]
         public float std_conc6;
         [FieldOffset(112)]
         public float std_conc7;
         [FieldOffset(116)]
         public float std_conc8;
         [FieldOffset(120)]
         public float dmax_limit;
         [FieldOffset(124)]
         public float saved_reagentblank;
         [FieldOffset(128)]
         public float std_concentration;
         [FieldOffset(132)]
         public float intercept;
         [FieldOffset(136)]
         public float slope;
         [FieldOffset(140)]
         public float delta_limit;//161 bytes
         [FieldOffset(144)]
         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
         public byte[] itemname;
         [FieldOffset(164)]
         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
         public byte[] controlname4QC;
         [FieldOffset(184)]
         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
         public byte[] reserved;

    }
     [StructLayout(LayoutKind.Explicit)]
      public struct test2
      {
         [FieldOffset(0)]
         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 200)]
         public byte[] BARRAY;
         [FieldOffset(0)]
         public test P;



      }
     private void button1_Click_1(object sender, EventArgs e)
    {

        test P;
        test2 TT;
        TT.P.itemcode = 123;
        TT.P.method = 12;
        TT.BARRAY = new byte[200];
        //TT.BARRAY[1] = 12;
    }

当我编写此代码时,我收到错误,无法从程序集'structuretest,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'加载类型'test2',因为它包含偏移0处的对象字段,即非对象字段错误对齐或重叠。我想为test2结构中的字节数组和第一个结构的对象设置offset0.pls给我一个解决方案..

1 个答案:

答案 0 :(得分:0)

是的,test2确实包含偏移0处的不兼容字段。byte[] BARRAY是对数组的引用; test P是一种值类型。那不行。您也许可以使用fixed缓冲区,但这是一个高级主题,可能不是您要表示的内容:

public fixed byte BARRAY[200];

在其他新闻中:可变结构和公共领域几乎总是一个坏主意。