class Program
{
/// <summary>
/// 分布式对象存储节点,可存储在内存或文件上。
/// </summary>
/// <param name="args"></param>
static unsafe void Main(string[] args)
{
CommandHead theCH;
byte[] Buffer = new byte[]{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };
fixed (byte* pt = Buffer)
{
CommandHead* cHP = (CommandHead*)(pt);
theCH = *(cHP);
}
Console.WriteLine(theCH.faceID);
Console.WriteLine(theCH.seriID);
for (int i = 0; i < Buffer.Length; i++)
{
Buffer[i] = 2;
}
Console.WriteLine(theCH.faceID);
Console.WriteLine(theCH.seriID);
Console.ReadKey();
}
{
/// <summary>
/// 分布式对象存储节点,可存储在内存或文件上。
/// </summary>
/// <param name="args"></param>
static unsafe void Main(string[] args)
{
CommandHead theCH;
byte[] Buffer = new byte[]{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };
fixed (byte* pt = Buffer)
{
CommandHead* cHP = (CommandHead*)(pt);
theCH = *(cHP);
}
Console.WriteLine(theCH.faceID);
Console.WriteLine(theCH.seriID);
for (int i = 0; i < Buffer.Length; i++)
{
Buffer[i] = 2;
}
Console.WriteLine(theCH.faceID);
Console.WriteLine(theCH.seriID);
Console.ReadKey();
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct CommandHead
{
public ushort seriID;
public ushort faceID;
public uint targetID;
}
经测试C#的解引用操作 有复制操作,所以,安全性可以保证。