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

标签 c#

我在 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/

相关文章:

c# - 调用 .SubmitChanges() linq 时获取随机堆栈溢出

c# - InterIMAP,在 C# 中查看未读 IMAP 邮件并下载附件

c# - 将图像转换为二进制?

c# - LINQ-to-SQL:ExecuteQuery(Type, String) 填充一个字段,但不填充另一个字段

c# - 在检索存储在特定字节中的值之前是否需要屏蔽数字?

c# - S3捆绑下载

c# - 适用于 .NET 4.0 的 Permcalc.exe

c# - C# 中 Func<in T, bool> 的运算符

c# - EmguCV - 运动检测不返回角度

c# - 为什么使用 .AsEnumerable() 而不是强制转换为 IEnumerable<T>?