从托管代码初始化非管理结构 (C#)

发布于 2024-08-22 09:16:30 字数 530 浏览 2 评论 0原文

我有一个 C++ 结构,我想在 C# 代码中反映它(检查所有字段并以特定顺序启动),我想将结构内存作为二进制数据转储到文件中。 如果我声明 int dummy_4[10] ,我在 struct 中的数组减速中遇到问题,编译器会引发无法混合托管 & 的错误。非托管类型。如果我将其定义为数组等...并在构造函数中初始化该数组,则该数组不会位于内存中的 dummy_3 之后,并且我无法将其转储到文件中。

需要一些如何解决它的想法。 谢谢

public ref struct Dummy_t
{
    int dummy_1;
    int dummy_2;
    int dummy_3;
//int dummy_4[2];  X compile error mix managed and unmanaged types
    array<int>^ dummy_4; 
    int dummy_5;
    Dummy_t()
    {
 dummy_4 = gcnew array<uint8_t>(2);
    }

};

I have a structure in C++ that I want to reflect in C# code (goover all field and initiate with specific order) that I want to dump the structure memeory as binary data into a file.
I have a problem in array decleration in the sturct if I declare int dummy_4[10] the compiler raise error that can't mix managed & unmanaged types. if I delare as array etc... and initialize the array in the constructor, the array doesn't locate after dummy_3 in the memory and I can't dump it to the file.

Neeed some ideas how to solve it.
Thanks

public ref struct Dummy_t
{
    int dummy_1;
    int dummy_2;
    int dummy_3;
//int dummy_4[2];  X compile error mix managed and unmanaged types
    array<int>^ dummy_4; 
    int dummy_5;
    Dummy_t()
    {
 dummy_4 = gcnew array<uint8_t>(2);
    }

};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分開簡單 2024-08-29 09:16:30

您可以在 Dummy_t 中编写自定义转储方法,该方法负责处理 dummy_4,并输出指向的数组中的值。

You can write a custom dump method in Dummy_t, which takes care about dummy_4, and outputs the values from the array pointed to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文