C++到C#数组声明

Ste*_*ger 1 .net c# marshalling

我想将以下代码转换为C#:

struct Elf32_Ehdr {
  uint8   e_ident[16];   // Magic number and other info
  uint16  e_type;        // Object file type
  uint16  e_machine;     // Architecture
  uint32  e_version;     // Object file version
  uint32  e_entry;       // Entry point virtual address
  uint32  e_phoff;       // Program header table file offset
  uint32  e_shoff;       // Section header table file offset
  uint32  e_flags;       // Processor-specific flags
  uint16  e_ehsize;      // ELF header size in bytes
  uint16  e_phentsize;   // Program header table entry size
  uint16  e_phnum;       // Program header table entry count
  uint16  e_shentsize;   // Section header table entry size
  uint16  e_shnum;       // Section header table entry count
  uint16  e_shstrndx;    // Section header string table index
};
Run Code Online (Sandbox Code Playgroud)

显然,它映射到不同的外壳.uint16 - > UInt16
uint32 - > UInt32
uint64 - > UInt64

显然,uint8映射到Byte.

问题是:

Byte   e_ident[16];   // Magic number and other info<br />
Run Code Online (Sandbox Code Playgroud)


不会编译,它说:数组大小不能在变量声明中声明...

什么,没有固定大小的数组没有新的?

将它映射到此是否正确:

Byte[] e_ident = new Byte[16];   // Magic number and other info
Run Code Online (Sandbox Code Playgroud)



或者结果是完全错误的?

Hen*_*man 5

您将需要Structlayout和MarshalAs属性,并使用类似下面的内容:

//untested
[Structlayout]
struct Elf32_Ehdr 
{
  [MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
  Byte   e_ident[16];   // Magic number and other info
  Uint16  e_type;       // Object file type
  ...
}
Run Code Online (Sandbox Code Playgroud)

但考虑到这是进一步搜索的暗示,这不是我所了解的很多东西.