在类上使用Marshal.SizeOf并在类上使用byte [] []类型数据时出错 [英] Error using Marshal.SizeOf on class with byte[][] type data on a class

查看:91
本文介绍了在类上使用Marshal.SizeOf并在类上使用byte [] []类型数据时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.

我正在使用此类:

Hi.

I''m using this class:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
    class THolidayTz
    {
        public ushort ID;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = 0)]
        public byte[][] HDate = new byte[4][]; // Error using Marshal.SizeOf
        public ushort TZID;
    };



当我使用Marshal.SizeOf:



When I use a Marshal.SizeOf:

byte[] buf = new byte[Marshal.SizeOf(Holiday)];



我收到此错误:

您不能将类型"THolidayTz"编组为非托管结构:无法计算任何移位或显着大小.

我看到使用byte [] []定义并使用以下错误时发生:



I got this error:

You can not marshal type "THolidayTz ''as unmanaged structure: one can not calculate any shift or significant size.

I see the error happens when I use a byte[][] definition, using:

public byte[]HDate = new byte[4];



我没有错误,但是我需要使用byte [] [] ...

谢谢.



I don''t Have error, but i Need to use byte[][]...

Thanks.

推荐答案

对于不容易映射到.NET类型的非托管类型,通常建议在编组和手动解析时将它们表示为IntPtr.通话完成后的数据.如果要提供非托管结构和功能存根,将很有帮助.
For unmanaged types that do no easily map to .NET types, it is generally recommended to represent them as IntPtr''s while marshalling and manually parse the data after the call completes. It would be helpful if you were to provide the unmanaged structure and function stub.



您需要这样做,

[MarshalAs(UnmanagedType.ByValArray,SizeConst = 4,ArraySubType = 0)]
公共字节[] [] HDate;

它会起作用.

因为,在C#.Net中,对于非托管类型,您不能像这样使用new ...
public byte [] HDate = new byte [4];

you need to do just like this,

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = 0)]
public byte[][] HDate;

it will work.

because, in C#.Net for unmanaged type you can not use new like this...
public byte[]HDate = new byte[4];


这篇关于在类上使用Marshal.SizeOf并在类上使用byte [] []类型数据时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆