我试图在结构内有一个堆栈分配的数组。我指的是指针。但我希望在没有额外代码的情况下完成分配,因为我在编写代码时知道大小(我不想做一堆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();
}
}
我最终只需要一个小小的改变!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)