Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;到C#数组声明_C#_.net_Marshalling - Fatal编程技术网

C++;到C#数组声明

C++;到C#数组声明,c#,.net,marshalling,C#,.net,Marshalling,我想将下面的代码转换为C#: 显然,它映射到不同的套管。 uint16->uint16 uint32->uint32 uint64->uint64 显然,uint8映射到字节。 问题是: Byte e_ident[16]; // Magic number and other info<br /> 或者这会证明是完全错误的吗?您将需要和marshallas属性,并使用类似于: //untested [Structlayout] struct Elf32_Ehdr {

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

显然,它映射到不同的套管。 uint16->uint16
uint32->uint32
uint64->uint64

显然,uint8映射到字节。

问题是:

Byte   e_ident[16];   // Magic number and other info<br />


或者这会证明是完全错误的吗?

您将需要和marshallas属性,并使用类似于:

//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
  ...
}

但这是一个进一步搜索的提示,这不是我所知道的很多东西。

你需要的是和MalSalas属性,并使用它类似于:

//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
  ...
}

但是考虑这是一个进一步搜索的提示,这不是我知道的很多。

你应该使用--虽然这要求结构在不安全代码中声明:

unsafe struct Elf32_Ehdr
{
    fixed byte e_ident[16];
    ushort type;
    // etc
}
您可能还需要
[StructLayout(StructLayoutKind.Sequential)]
[StructLayout(StructLayoutKind.Explicit)]
。基本上,固定大小的缓冲区确保数据是内联的,而不是创建需要巧妙编组的单独数组。

您应该使用-尽管这需要在不安全的代码中声明结构:

unsafe struct Elf32_Ehdr
{
    fixed byte e_ident[16];
    ushort type;
    // etc
}

您可能还需要
[StructLayout(StructLayoutKind.Sequential)]
[StructLayout(StructLayoutKind.Explicit)]
。基本上,固定大小的缓冲区确保数据是内联的,而不是创建需要巧妙编组的单独数组。

它不编译,但却是投票率最高的答案。奇怪的地方,不是吗?@Hans,这是记忆中的,这就是//未经测试的旗帜的用途。我从未声称/假装过。不过,它似乎起到了指针的作用。你只是迟到了3个小时(:。我知道,今天下午有点暴躁。删除了一堆好的答案,编译过的东西。抱歉!它没有编译,但投票率最高。奇怪的地方,不是吗?@Hans,它来自记忆,这就是//untested标志的作用。我从来没有声称/假装编译过它。但它看起来像是一个指针。而你只需要3个小时te(-:。我知道,今天下午有点暴躁。删除了一堆好的答案,编译的东西。对不起!