日期:2014-05-20  浏览次数:20805 次

如何获取这个自定义结构体的大小
有一个自定义结构体:
C# code

public struct TimeYS                    
{
    public int Year;
    public int Month;
    public int Day;
    public int Hour;
    public int Minute;
    public int Second;
}
public struct BExtrTypeHrmnCntRt
{
    public TimeYS Tm;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=51)]
    public float[]   MxAN;        //A相最大含有率
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=51)]
    public TimeYS[]  MxTmAN;      //A相最大含有率发生时间
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=51)]
    public float[]   MnAN;        //A相最小含有率
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=51)]
    public TimeYS[]  MnTmAN;      //A相最小含有率发生时间
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=51)]
    public float[]   MxBN;        //B相最大含有率
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=51)]
    public TimeYS[]  MxTmBN;      //B相最大含有率发生时间
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=51)]
    public float[]   MnBN;        //B相最小含有率
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=51)]
    public TimeYS[]  MnTmBN;      //B相最小含有率发生时间
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=51)]
    public float[]   MxCN;        //C相最大含有率
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=51)]
    public TimeYS[]  MxTmCN;      //C相最大含有率发生时间
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=51)]
    public float[]   MnCN;        //C相最小含有率
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=51)]
    public TimeYS[]  MnTmCN;      //C相最小含有率发生时间
}



当用Marshal.SizeOf(typeof(BExtrTypeHrmnCntRt))获取这个结构体长度时,却提示错误,请问该如何获取这样的结构体长度,或者说将这种结构体转成byte数组?谢谢

------解决方案--------------------
vs2005 .NET2.0编译通过!
C# code
using System;
using System.Collections.Generic;
using System.Text;
[color=#FF0000]using System.Runtime.InteropServices;[/color]

namespace ConsoleApplication1
{
    public struct TimeYS
    {
        public int Year;
        public int Month;
        public int Day;
        public int Hour;
        public int Minute;
        public int Second;
    }

    public struct BExtrTypeHrmnCntRt
    {
        public TimeYS Tm;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 51)]
        public float[] MxAN;        //A相最大含有率
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 51)]
        public TimeYS[] MxTmAN;      //A相最大含有率发生时间
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 51)]
        public float[] MnAN;        //A相最小含有率
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 51)]
        public TimeYS[] MnTmAN;      //A相最小含有率发生时间
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 51)]
        public float[] MxBN;        //B相最大含有率
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 51)]
        public TimeYS[] MxTmBN;      //B相最大含有率发生时间
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 51)]
        public float[] MnBN;        //B相最小含有率
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 51)]
        public TimeYS[] MnTmBN;      //B相最小含有率发生时间
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 51)]
        public float[] MxCN;        //C相最大含有率
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 51)]
        public TimeYS[] MxTmCN;      //C相最大含有率发生时间
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 51)]
        public float[] MnCN;        //C相最小含有率
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 51)]
        public TimeYS[] MnTmCN;      //C相最小含有率发生时间
    }


    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Marshal.SizeOf(typeof(BExtrTypeHrmnCntRt))); //输出8592
            Console.ReadLine();
        }
    }
}