我正在使用其他人编写的项目从Parrot AR Drone接收一些数据。很多数据以字节数组的形式出现,我使用的这个库使用一堆结构进行分析。一般来说,我对编组真的很陌生。摆脱不安全的代码,从字节编组uint数组?
我有一个结构,看起来像这样:
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public unsafe struct navdata_vision_detect_t
{
public ushort tag;
public ushort size;
public uint nb_detected;
public fixed uint type [4]; // <Ctype "c_uint32 * 4">
public fixed uint xc [4]; // <Ctype "c_uint32 * 4">
public fixed uint yc [4]; // <Ctype "c_uint32 * 4">
public fixed uint width [4]; // <Ctype "c_uint32 * 4">
public fixed uint height [4]; // <Ctype "c_uint32 * 4">
public fixed uint dist [4]; // <Ctype "c_uint32 * 4">
public fixed float orientation_angle [4]; // <Ctype "float32_t * 4">
}
但是,如果我曾经试着访问navdata_vision_detect_t的实例,并在固定的uint值获得,我必须使用“固定”关键字,它似乎真的很乱:
unsafe private void drawTagDetection()
{
int x, y;
if (_detectData.nb_detected > 0)
{
fixed (uint* xc = _detectData.xc)
{
x = (int)xc[0];
}
fixed (uint* yc = _detectData.yc)
{
y = (int)yc[0];
}
}
我希望能够只访问UINT阵列就像我会正常的C#阵列。我认为我应该能够使用编组,但我无法使它工作。我想是这样的:
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public uint[] type; // <Ctype "c_uint32 * 4">
这让我删除“不安全”和“固定”的关键字,但引起了另一个问题,因为解析字节的数据时,有一个大的switch语句,做一些蒙上各种结构像这样:
private static unsafe void ProcessOption(navdata_option_t* option, ref NavdataBag navigationData){
var tag = (navdata_tag_t) option->tag;
switch (tag)
{
//lots of other stuff here
case navdata_tag_t.NAVDATA_VISION_TAG:
navigationData.vision = *(navdata_vision_t*) option;
break;
}
}
所以我仍然有一些指向这个结构在另一个不安全的函数。我怎样才能让这些结构中的数组保持“安全”,同时又允许另一个不安全的函数将我的对象作为结构体来施放?
感谢您给予任何帮助!
不幸的是,您的问题和代码示例并不完全清楚。为什么你的'ProcessOption()'方法将一个指针作为参数?什么是'navdata_option_t'类型?这与“navdata_vision_detect_t”类型有什么关系?'navdata_vision_detect_t'类型有一个'size'字段;这个大小实际上是可变的?您使用的图书馆是否强迫您使用不安全的结构,或者您是否拥有对图书馆代码的控制权?在我看来,仅仅使用'BitConverter'来解析数组到实际的结构将是最好的。 – 2015-02-10 07:58:35
请注意,通过在类型中使用显式布局并将多个字段(本身可以是结构体)的偏移量设置为结构中的相同位置,您可以在C#中有效地创建联合。这个问题对你是否有用尚不清楚,因为问题本身不是很清楚。 – 2015-02-10 07:59:35
'* _detectData.xc'不工作?编辑:不,你正在使用它作为一个领域。作为一个本地人,你不需要那个。所以修复它在入门或使用参数等 – leppie 2015-02-10 18:25:35