gpt4 book ai didi

c# - 无法编码结构的字段

转载 作者:行者123 更新时间:2023-11-30 21:35:06 25 4
gpt4 key购买 nike

我在 dll 中有一个结构,类型如下。但是当我调试它时,出现以下异常。请帮助解决这个问题。

Cannot marshal field 'id' of type 'RCMMMResult_S': The type definition of this field has layout information but has an invalid managed/unmanaged type combination or is unmarshalable.

[StructLayout(LayoutKind.Sequential)]
public struct RCMMMResult_S
{

public RCMMMResultID_s id;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] value;
// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public byte status;

public RCMMMResult_S(bool init = true)
{

id = new RCMMMResultID_s();
value = new byte[4];
status = 0;
}
} ;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
[Serializable]
public struct RCMMMResultID_s
{

[MarshalAs(UnmanagedType.I1, SizeConst = 1)]
public byte analyte_id;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public byte unit_code;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public byte[] variant;
public RCMMMResultID_s(bool init = true)
{
analyte_id = 0;
unit_code = 0;
variant = new byte[2];
}
} ;

最佳答案

问题出在这里:

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public byte unit_code;

这是一个单字节,不能用作 by-val 数组。 I1 可以正常工作。

关于c# - 无法编码结构的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49531585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com