这个c# 代码片段使用ExplicitLayout attribute 来创建一个类似c 的union
using System.Runtime.InteropServices;
...
[StructLayout(LayoutKind.Explicit)]
internal struct Union
{
[FieldOffset (6)] internal byte byteData;
[FieldOffset (0)] internal string stringText;
[FieldOffset (4)] internal short unionShort;
[FieldOffset (4)] internal byte lowByte;
[FieldOffset (5)] internal byte highByte;
}
public class TestUnion
{
public static void Main( )
{
Union union = new Union ();
union.stringText = "Union";
union.byteData = 0xFF;
union.lowByte = 0x01;
union.highByte = 0x01;
Console.WriteLine (union.unionShort + " = " +
(union.highByte * 256 + union.lowByte).ToString());
}
}
标签:
代码
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。