结构中的UNION [英] UNION in struct

查看:109
本文介绍了结构中的UNION的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些来自C / C ++的代码,我试图移植到C#。我已经来了一个有趣的问题,这在复杂的C / C ++代码中非常常见:

UNION的结构定义允许同一块内存

作为不同的数据类型引用。这通常用于节省空间

并允许单个内存包含不同类型的数据,

通常基于结构中的其他标志。在其他情况下,这个

只允许以不同的方式查看数据 - 例如,作为16个32位整数的

数组或64个数组字节。由于这是在编译时定义的
,因此在执行任何

转换等时没有运行时开销。


如何我能在C#中实现同样的目标吗?我现在对后者

的情况更感兴趣 - 能够查看同一块内存为

是一个16int的数组。或74字节或对象。


-ken

I have some code from C/C++ that I am attempting to port to C#. I have come
across an interesting problem that is quite common in complex C/C++ code:
the us of UNION in structure definitions to permit the same piece of memory
to be referenced as different data types. This is often used to save space
and permit a single piece of memory to contain different types of data,
typically based on some other flag in the structure. In other cases this
simply permits the data to be viewed in different ways -- for example, as an
array of 16 32-bit integers or as an array of 64 bytes. Since this is
defined at compile time, there is no runtime overhead in performing any
conversions or the like.

How can I achieve the same thing in C#? I am more interested in the latter
situation right now -- the ability to view the same piece of memory as
either an array of 16 "int" or 74 "byte" objects.

-ken

推荐答案

C#中没有联盟,你可以试试使用StructLayout属性和FieldOffset attribut


[StructLayout(LayoutKind.Explicit)

class MyUnio


[FieldOffset (0)]字节byte1

[FieldOffset(1)] byte byte2

[FieldOffset(2)] byte byte3

[FieldOffset(3 )] byte byte4

[FieldOffset(0)] int myInt

但我不认为它适用于数组,因为它是参考类型。
there''s no union in C#, you can try to use the StructLayout attribute and FieldOffset attribut

[StructLayout( LayoutKind.Explicit)
class MyUnio

[FieldOffset(0)] byte byte1
[FieldOffset(1)] byte byte2
[FieldOffset(2)] byte byte3
[FieldOffset(3)] byte byte4
[FieldOffset(0)] int myInt
but I don''t think it will work with array since it''s a reference type.


" Daniel Jin" <一个******* @ discussions.microsoft.com>在留言中写道

新闻:F4 ********************************** @ microsof t.com ...
"Daniel Jin" <an*******@discussions.microsoft.com> wrote in message
news:F4**********************************@microsof t.com...
C#中没有联合,你可以尝试使用StructLayout属性和
FieldOffset属性
[StructLayout(LayoutKind.Explicit)]
类MyUnion
{FieldOffset(0)] byte byte1;
[FieldOffset(1)] byte byte2;
[FieldOffset(2)] byte byte3;
[FieldOffset(3)] byte byte4;
[FieldOffset(0)] int myInt;
}

但我认为它不会起作用数组,因为它是一个引用类型。
there''s no union in C#, you can try to use the StructLayout attribute and FieldOffset attribute
[StructLayout( LayoutKind.Explicit)]
class MyUnion
{
[FieldOffset(0)] byte byte1;
[FieldOffset(1)] byte byte2;
[FieldOffset(2)] byte byte3;
[FieldOffset(3)] byte byte4;
[FieldOffset(0)] int myInt;
}

but I don''t think it will work with array since it''s a reference type.




我不想实际上是一个联盟,我知道它不存在。

这个结构没有与任何代码共享,它只是在一些

计算中使用。


我需要一种方法来声明一个数组整数然后能够引用

这是一个字节数组或长数组。在我的例子中,这个结构的长度是

总是64字节,所以我希望能够将它引用为64

字节,4个整数或2个长整数。 />

-ken



I do not want to actually us a union, and I am aware that it does not exist.
This structure is not being shared with any code, it is simply used in some
calculations.

I need a way to declare an array of integers and then be able to reference
this as either an array of bytes or longs. The lenth of this structure is
always 64 bytes in my example, so I want to be able to reference it as 64
bytes, 4 ints or 2 longs.

-ken


在同一个注释中,如何在结构中声明一个数组并拥有它

占用结构中的空间 - 我的印象是

中的声明结构将是对数组的引用,该数组在其他地方分配。


-ken
On this same note, how does one declare an array in a struct and have it
take space within the struct -- my impressions is that the declaration in
the struct will be a reference to the array, which is allocated elsewhere.

-ken


这篇关于结构中的UNION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆