Kra*_*ken 3 c# unsafe indexer this unsafe-pointers
我正在为我们的代码库中的性能/内存关键部分试验数据结构.我想快速访问结构中定义的字节.但是我不知道如何使用索引器访问我正在操作的结构.
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Foo
{
    [SerializeField]
    private byte a, b, c;
    public unsafe byte this[byte index]
    {
        get
        {       
            //omitted safety checks   
            //this is a no, no
            byte* addr = (byte*)&this;
            return addr[index];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
    你只能在一个fixed街区内做你想做的事情,即:
fixed (Foo* foo = &this)
{
    byte* addr = (byte*)foo;
    return addr[index];
}
Run Code Online (Sandbox Code Playgroud)