如何在 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# 中那么容易地做到这一点。
请注意,C++ 结构中有一个数组。
或者,使用 此源文件 可以为我们完成这项工作,但如果结构中有数组则不行。
C++ code:
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;
We can't do that as easy in 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定你到底在问什么。 您是否正在尝试在 C# 中获取等效的结构定义以用于普通的旧 C# 用法或用于互操作 (P/Invoke) 目的? 如果它用于 P/Invoke,则以下结构将起作用。
如果您正在寻找具有相同特征的普通旧 C# 结构,则不幸的是不可能使用结构来实现。 您不能在 C# 结构中定义恒定大小的内联数组,也不能通过初始值设定项强制该数组为特定大小。
托管世界中有两种替代选择。
使用具有填充数组的 create 方法的结构
,或者使用可以直接初始化 array123 成员变量的类。
编辑 OP watns 了解如何将 byte[] 转换为 tPacket 值
不幸的是,在 C# 中没有很好的方法来做到这一点。 C++ 非常适合此类任务,因为它的类型系统非常弱,您可以选择将字节流视为特定结构(邪恶的指针转换)。
这在 C# 不安全代码中可能是可能的,但我不相信它是这样。
本质上,您要做的就是手动解析字节并将它们分配给结构中的各个值。 或者编写一个本机方法,将 C 风格转换和 PInvoke 转换为该函数。
I'm unsure of exactly what you are asking. Are you trying to get an equivalent structure definition in C# for plain old C# usage or for interop (P/Invoke) purposes? If it's for P/Invoke the following structure will work
If you are looking for a plain old C# structure that has the same characteristics, it's unfortunately not possible to do with a struct. You cannot define an inline array of a contstant size in a C# structure nor can you force the array to be a specific size through an initializer.
There are two alternative options in the managed world.
Use a struct which has a create method that fills out the array
Or alternatively use a class where you can initialize the array123 member variable directly.
EDIT OP watns to know how to convert a byte[] into a tPacket value
Unfortunately there is no great way to do this in C#. C++ was awesome for this kind of task because has a very weak type system in that you could choose to view a stream of bytes as a particular structure (evil pointer casting).
This may be possible in C# unsafe code but I do not believe it is.
Essentially what you will have to do is manually parse out the bytes and assign them to the various values in the struct. Or write a native method which does the C style casting and PInvoke into that function.
您正在寻找的内容(如果您使用类似@JaredPar 发布的类似结构定义)是这样的:
What you are looking for (if you are using a similar structure definition like @JaredPar posted) is something like this:
它也可以使用不安全的代码来完成,尽管它限制了程序可以运行的上下文,并且自然地引入了安全缺陷的可能性。
优点是您可以使用指针直接从数组转换为结构,并且如果您只想在结构中添加或删除字段,那么它也是免维护的。 但是,访问数组需要使用固定语句,因为当结构体包含在对象中时,GC 仍然可以在内存中移动该结构体。
以下是我用于解释 UDP 数据包的不安全结构的一些修改代码:
对于此类代码,检查数组的长度是否与结构的大小完全匹配非常重要,否则您将面临一些令人讨厌的缓冲区溢出。 由于 unsafe 关键字已应用于整个结构,因此您无需将每个方法或代码块标记为单独的 unsafe 语句。
It can be done with unsafe code too, although it restricts the context under which your program can run, and, naturally, introduces the possibility of security flaws.
The advantage is that you cast directly from an array to the structure using pointers and it's also maintenance-free if you are only going to add or remove fields from the struct. However, accessing the arrays require using the fixed-statement as the GC can still move the struct around in memory when it's contained in an object.
Here's some modified code of an unsafe struct I used for interpreting UDP packets:
For this kind of code it is extremely important to check that the length of the array perfectly matches the size of the struct, otherwise you'll open for some nasty buffer overflows. As the unsafe keyword has been applied to the whole struct, you don't need to mark each method or codeblock as separate unsafe statements.
您可以通过在结构中编写用于访问的函数,将外界看起来像固定大小的数组的内容放置在安全结构中。 例如,以下是安全结构内的固定 4 x 4 双精度数组:
You can place what looks to the outside world like an array of fixed size within a safe structure by writing functions within the structure for access. For example, here is a fixed 4 by 4 double precision array within a safe structure: