无法将uint*转换为uint []

Chr*_*ens 7 c# arrays pointers unsafe

我有这个不编译的代码:

public struct MyStruct
{
    private fixed uint myUints[32];
    public uint[] MyUints
    {
        get
        {
            return this.myUints;
        }
        set
        {
            this.myUints = value;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我知道为什么代码不会编译,但我显然是在我太累了想不到的地方,需要一些帮助才能让我朝着正确的方向前进.我有一段时间没有处理不安全的代码,但我很确定我需要做一个Array.Copy(或Buffer.BlockCopy?)并返回一个数组的副本,但是那些不需要我需要的参数.我忘记了什么?

谢谢.

jas*_*son 5

fixed使用fixed缓冲区时,您必须在上下文中工作:

public unsafe struct MyStruct {
    private fixed uint myUints[32];
    public uint[] MyUints {
        get {
            uint[] array = new uint[32];
            fixed (uint* p = myUints) {
                for (int i = 0; i < 32; i++) {
                    array[i] = p[i];
                }
            }
            return array;
        }
        set {
            fixed (uint* p = myUints) {
                for (int i = 0; i < 32; i++) {
                    p[i] = value[i];
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)