怎样为在Struct中为二维数组设定SizeConst

EchoJia 2003-12-09 11:24:52
各位大侠:
小弟遇到一个问题,我定义的Struct如下
[StructLayout(LayoutKind.Sequential)]
public struct SCommPackage
{
public UInt16 Channel;
public UInt16 sChannel;
public UInt16 CommEvent;
public Int16 Parameter;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)]
public Int16[] iData;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=320)]
public char[,] sData;
}
执行下面的代码时会出错,从结构体中去掉那个二维数组则没有问题
this.package = new SCommPackage();
int size = Marshal.SizeOf(this.package);
我想应该是SizeConst设置的问题,但不知道如何解决,
欢迎各位大侠前来指教。

...全文
151 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
EchoJia 2003-12-15
  • 打赏
  • 举报
回复
我试了很久,还是不行,
谢谢夕夕公主!
xixigongzhu 2003-12-11
  • 打赏
  • 举报
回复
他没有提供这样的接口,再怎么搞都不行。
EchoJia 2003-12-09
  • 打赏
  • 举报
回复
现在的情况是这个接口已经确定了,没有办法改动,

你是说在C#中无法为二维数组设定SizeConst吗?
xixigongzhu 2003-12-09
  • 打赏
  • 举报
回复
只有将二维数组定义成一维:
[StructLayout(LayoutKind.Sequential)]
public struct SCommPackage
{
public UInt16 Channel;
public UInt16 sChannel;
public UInt16 CommEvent;
public Int16 Parameter;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)]
public Int16[] iData;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=320)]
public char[] sData;
}
EchoJia 2003-12-09
  • 打赏
  • 举报
回复
我顶!
EchoJia 2003-12-09
  • 打赏
  • 举报
回复
有人吗?
难道是我写的不够清楚

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧