本文介绍了我怎样才能把一个数组C#中的结构里面呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
C ++代码:
struct tPacket { WORD word1; WORD word2; BYTE byte1; BYTE byte2; BYTE array123[8]; } static char data[8192] = {0}; ... some code to fill up the array ... tPacket * packet = (tPacket *)data;我们不能这样做,在C#中那样容易。
We can't do that as easy in C#.
请注意,在C ++结构数组。
Please note there is an array in the C++ structure.
另外,使用的这个源文件能为我们做的工作,但如果有一个数组在结构上。
Alternatively, using this source file could do the job for us, but not if there is an array in the structure.
推荐答案我想你正在寻找(如果您使用的是类似的结构定义,就像JaredPar贴)是是这样的:
I think what you are looking for (if you are using a similar structure definition like JaredPar posted) is something like this:
tPacket t = new tPacket(); byte[] buffer = new byte[Marshal.SizeOf(typeof(tPacket))]; socket.Receive(buffer, 0, buffer.length, 0); GCHandle pin = GCHandle.Alloc(buffer, GCHandleType.Pinned); t = (tPacket)Marshal.PtrToStructure(pin.AddrOfPinnedObject(), typeof(tPacket)); pin.free(); //do stuff with your new tPacket t更多推荐
我怎样才能把一个数组C#中的结构里面呢?
发布评论