本文介绍了试图将一些C ++结构移植到C#问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试将一些C ++结构移植到C#但我遇到麻烦这个 一个。 注意:这是一个文件记录,所以我必须保留这个格式。 #pragma pack(push,1) typedef struct { char title [80]; DWORD dwCount; } STLHEADER,* LPSTLHEADER; #pragma pack(pop) 使用Google我发现这可以移植到这个C#变体, 注意''Marshal''来创建一个80字节的数组。 [StructLayout(LayoutKind.Sequential,Pack = 1)] 内部结构STLHEADER { [MarshalAs(UnmanagedType.U1,SizeConst = 80)] public char [] title; public int dwCount; } 到目前为止一切顺利,但现在我被困在这条线上sizeof(STLHEADER)需要 不安全的功能。 错误CS0208:无法获取托管 类型变量的地址或大小(''MySpace .STLHEADER'') 它与Marshal的事情有关。 我可以替换sizeof(STLHEADER)。 80 + 4,但我真的更喜欢 来获得更专业的解决方案。 也许还有另一种方法来定义''char title [80]''字节 数组? 感谢您的任何反馈。 :-) - http:/ /www.skyscan.be I am trying to port some C++ structures to C# but I have troubles with thisone.Note: this is a file record, so I must keep this format. #pragma pack( push, 1 )typedef struct {char title[80]; DWORD dwCount;} STLHEADER, *LPSTLHEADER;#pragma pack( pop ) Using Google I discovered that this could be ported to this C# variant,note the ''Marshal'' to create a 80 byte array. [StructLayout(LayoutKind.Sequential, Pack=1)]internal struct STLHEADER {[MarshalAs (UnmanagedType.U1, SizeConst=80)]public char[] title; public int dwCount;} So far so good, but now I am stuck on this line "sizeof(STLHEADER)" neededin a unsafe function.error CS0208: Cannot take the address or size of a variable of a managedtype (''MySpace.STLHEADER'') And it has something to do with the Marshal thing.I could replace the "sizeof(STLHEADER)" with 80+4 but I would really preferto have a more professional sollution.Maybe there is another alternative way to define a ''char title[80]'' bytearray? Thanks for any feedback. :-) -- http://www.skyscan.be推荐答案 这篇关于试图将一些C ++结构移植到C#问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-12 10:23