声明不安全的固定自定义结构数组的解决方法? [英] Workaround on declaring a unsafe fixed custom struct array?
问题描述
对于错误 CS1663(固定大小的缓冲区类型必须是以下之一:bool、byte、short、int、long、char、sbyte、ushort、uint、ulong、float 或 double.")是否有任何解决方法.?
Is there any workaround for the error CS1663 ("Fixed size buffer type must be one of the following: bool, byte, short, int, long, char, sbyte, ushort, uint, ulong, float or double.")?
我需要从另一个 blittable 自定义类型结构声明一个不安全的固定数组,但我陷入了这个编译器错误.
I need to declare a unsafe fixed array from another blittable custom type struct but I'm stuck in this compiler error.
显示一些代码来阐明下面的问题.
Showing some code to elucidate the problem below.
struct s1
{
byte _b1;
byte _b2;
}
unsafe struct s2
{
fixed s1 _s1[5]; // CS1663 here...
}
请注意,这两个结构体是 blittable,因此该错误对我没有任何意义.
Note that the two structs are blittable, so the error doesn't make any sense for me.
有人知道我能做什么吗?
Anyone have any idea about what I could do?
谢谢.
推荐答案
另一种解决方法是使用指针类型字段.
Another workaround is to either use pointer type fields.
public struct Foo
{
public byte _b1;
public byte _b2;
}
public unsafe struct Bar
{
public int Length;
public unsafe Foo* Data;
}
或者,如果您的意图是连续创建一个包含其所有数据的单一值类型对象(即,它可以在非托管代码中被 memcpy'd)——那么唯一的方法是通过根本不使用任何数组.
Alternately, if your intention was to make a single value type object which contains all of its own data, contiguously (ie, so it can be memcpy'd in unmanaged code) -- then the only way to do that is by not using any arrays at all.
public struct Foo
{
public byte _b1;
public byte _b2;
}
public struct Bar1
{
public Foo F1;
}
public struct Bar2
{
public Foo F1;
public Foo F2;
}
public struct Bar3
{
public Foo F1;
public Foo F2;
public Foo F3;
}
这篇关于声明不安全的固定自定义结构数组的解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!