我怎样才能把一个数组C#中的结构里面呢?

编程入门 行业动态 更新时间:2024-10-10 08:22:36
本文介绍了我怎样才能把一个数组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#中的结构里面呢?

本文发布于:2023-11-29 13:29:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646522.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   结构

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!