如何在 C# 中使用结构数组
How use Struct Array in C#
我是一个新的C#用户。我在下面有一个 C/C++ 结构:
typedef struct
{
float x;
float y;
}Point2f;
typedef struct
{
int id;
unsigned char charcode;
}CharResultInfo;
typedef struct
{
int strlength;
unsigned char strval[1024];
CharResultInfo charinfo[1024];
}StringResultInfo;
typedef struct
{
int threshold;
int polarity;
bool inverted;
}Diagnotices;
typedef struct
{
Point2f regioncenter;
StringResultInfo stringinfo;
Diagnotics diagnotics;
}SingleOutResult;
我使用 C# 定义相同的结构,如下所示:
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct Point2f
{
public double x;
public double y;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public unsafe struct DF_TAdvOCRCharResultInfo
{
public Int32 id;
public char charcode;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public unsafe struct DF_TAdvOCRStringResultInfo
{
public int strlength;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 1024)]
public string strval;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 1024, ArraySubType = UnmanagedType.Struct)]
public CharResultInfo[] charinfo;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public unsafe struct Diagnotics
{
public Int32 polarity;
[MarshalAsAttribute(UnmanagedType.I1)]
public bool inverted;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public unsafe struct OutResult
{
public Point2f regioncenter;
public StringResultInfo stringinfo;
public Diagnotics diagnotics;
}
但是,当我在 C# 项目中使用以下项目时:
OutResult *pResult = (OutResult *)inputparam; //inputparam input from C/C++ dll
编译器输出:
错误 CS0208:无法获取 的地址、获取大小或声明 指向托管类型 ('***.结果')
我的问题是为什么结构指针无法使用以及如何修复?
指针不能指向引用或包含引用的结构,因为即使指针指向对象引用,也可以对对象引用进行垃圾回收。垃圾回收器不会跟踪对象是否由任何指针类型指向
。
https://msdn.microsoft.com/en-us/library/y31yhkeb.aspx
本质上,因为 C# 是一种托管语言,所以它需要跟踪对对象的所有引用,以便知道何时对其进行 GC。如果在 OutResult 中声明指向诊断对象的指针,则 GC 将不知道新引用,并且稍后可以在使用对象时释放对象。
为了解决这个问题,我会亲自避开指针,除非你绝对必须使用它们。我不确定你的整个程序是什么,但如果你只是想传递引用,那么让OutResult成为引用类型(类)而不是值类型(结构)。C# 是一种托管语言,因此最好尝试并坚持使用托管上下文,特别是如果您还是初学者的话。
public class OutResult
{
public Point2f regioncenter;
public StringResultInfo stringinfo;
public Diagnotics diagnotics;
}
相关文章:
- C++将文本文件中的数据读取到结构数组中
- JSON转换为nlohmann JSON-lib中的结构数组
- 需要帮助将结构数组传递给函数
- 在 64 位边界上对齐C++结构数组?
- 带有枚举方向/类型的气泡排序结构数组
- 在C++中访问结构数组中的数据
- 从指向结构数组的指针中提取元素
- 如何制作一个地图,其中的值是C++中的结构数组
- 如何在函数中显示结构数组
- 包含作为成员C++函数的结构数组
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- std::transform 将一个结构数组复制到另一个结构数组
- 将非托管结构数组封送到托管数组
- 显示结构数组
- 结构数组中所有类似字段的总和
- 结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配
- 在 glsl opengl 中将嵌套结构数组作为统一传递
- 使用初始化列表填充C++中的多维结构数组时出现问题
- 如何将字符串字符与结构数组进行比较?
- C++结构数组