问个marshal问题

来源:百度知道 编辑:UC知道 时间:2024/06/09 23:02:51
struct A
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
int []a;
};

struct AA
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
A []aa;
};

使用Marshar.Sizeof(typeof(AA))怎么不行呢? 报告notsupportexception急问,谢谢!

根据MSDN帮助知道Marshar.Sizeof()参数要给的是对象的实例。

AA只是对象名,不是实例

这样才是正确的.
AA value;
Marshar.Sizeof(value)

至于为什么就要说clr了。

给 A 加上 Attribute
[StructLayout(LayoutKind.Sequential, Size=40)]

AA 加上
[StructLayout(LayoutKind.Sequential)]

即可