[问题]C# 结构体对齐:如何将变长byte数组对齐

[StructLayout(LayoutKind.Sequential,Pack=1)] 
struct Report_Read_Parameter 
{ 
    byte Confirmation; 
    byte ListID; 
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] 
    byte[] ParameterID; 
    byte Length; 
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = ***)] // based on the Length 
    byte[] ParameterValue; 
    byte Status; 
}

问题来了,怎么根据结构体里的字段Length来确定byte数组ParameterValue 的长度?

如何实现动态对齐?

 

在网上查了一下,但凡是声明为ByValArray类型,就必须要跟上SizeConst,而且设置SizeConst=Length时,编译器会报错

"An object reference is required for the non-static field, method, or property 'NGK.BLL.Commands.Report.Report_Read_Parameter.Length'"

posted @ 2014-12-19 10:16  我不是杰克船长  阅读(1557)  评论(0编辑  收藏  举报