簡體   English   中英

元帥大小常量數組

[英]Marshal size const array

我正在嘗試在結構中分配堆棧數組。 好吧,我的意思是指針。 但是我希望在沒有額外代碼的情況下完成分配,因為我在編寫代碼時知道大小(我不想在創建結構時做一堆new的)。 如果我什至可以在沒有unsafe上下文的情況下做到這一點,那就太完美了。 我嘗試了一些東西,但效果不佳。 我是 C# 的新手,所以可能有一種我沒見過的方法!

public struct TestValue {int value; }

[StructLayout(LayoutKind.Sequential)]
public struct TestArray {
   [MarshalAs(UnmanagedType.ByValArray, SizeConst=128)] public TestValue[] s1;
}

public struct TestSpan
{
    Span<TestValue> data= stackalloc TestValue[10];
}
using System.Runtime.InteropServices;

public struct TestValue {int value; }

[StructLayout(LayoutKind.Sequential)]
public struct TestArray {
   [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=128)] public TestValue[] s1;
}

public class Foo
{
    void test()
    {
        TestArray test = new TestArray();
        test.s1[10] = new TestValue();
    }
}

最后我只需要一點點改變!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM