如何从结构中使用结构类型的参数?

时间:2017-06-13 06:23:47

标签: c#

我在结构中声明了结构类型的参数,当我尝试从结构中使用该参数时,我得到错误TCommandParam

此外,我知道您无法在结构中初始化结构。有没有办法使用该参数而不会出现该错误?

我想使用TCommandBuffer struct。{/ p>中的[StructLayout(LayoutKind.Sequential)] public struct TCommandParam { public int iValue; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4096)] public byte[] sValue; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)] public byte[] FrameFD; } [StructLayout(LayoutKind.Sequential)] public struct TCommandBuffer { public int Command; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)] public TCommandParam[] Param; public int ReturnValue; } 参数

PdfWriter

1 个答案:

答案 0 :(得分:0)

也许使用构造函数?修改第二个结构如下:

[StructLayout(LayoutKind.Sequential)]
public struct TCommandBuffer
{
    public int Command;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
    public TCommandParam[] Param;
    public int ReturnValue;
    public TCommandBuffer(int tsize, int cmd, int ret)
    {
        Param = new TCommandParam[tsize];
        Command = cmd;
        ReturnValue = ret;
    }
}