我在SO和其他论坛上看到了几个相关主题,但没有找到可行的答案来回答我的问题。
这是我的代码:
[StructLayout(LayoutKind.Explicit, Size = 128)]
internal unsafe struct Frame
{
[FieldOffset(0)]
public fixed byte Bytes[128];
[FieldOffset(0)]
public long Low;
[FieldOffset(128 - sizeof(long))]
public long High;
}
unsafe private void button32_Click(object sender, EventArgs e)
{
Frame frame;
// ERROR: Error 15 You cannot use the fixed statement to take the address of an already fixed expression
fixed (byte* ptr = frame.Bytes)
{
}
// ERROR
Console.Write(System.Text.Encoding.ASCII.GetString(frame.Bytes, 0, 128));
frame.Low = 1234;
//Console.Write(System.Text.Encoding.ASCII.GetString(frame.Bytes));
frame.High = 5678;
//Console.Write(System.Text.Encoding.ASCII.GetString(frame.Bytes));
}
start
和length
重载),那么new string((sbyte*) frame.Bytes)
应该可以正常工作。 - leppie