none
How to get the total byte size in a class? RRS feed

  • Question

  • Dear All,

    How to get the total size of this class programmatically?

    Thanks and Best regards,

    E-John

        [StructLayout(LayoutKind.Sequential)]
        public class TxDataStruct
        {
            public UInt32 ID;
            public ushort Number;
            public byte[] Name = new byte[20];
            public byte[,] NameBitMap = new byte[20, 32];;
            public byte[] Attribute = new byte[6];
            public byte[] Company = new byte[8];
        }

    Wednesday, July 25, 2018 5:14 AM

Answers

  • But the size of each byte[] is 4 (in 32-bit)

    To get 680, you can do something like :

            [StructLayout(LayoutKind.Sequential)]
            public class TxDataStruct
            {
                public UInt32 ID;
                public ushort Number;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
                public byte[] Name;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20*32)]
                public byte[,] NameBitMap;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
                public byte[] Attribute ;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
                public byte[] Company;
            }

    • Marked as answer by E-John Thursday, July 26, 2018 3:24 AM
    Wednesday, July 25, 2018 9:33 AM

All replies

  • You can use :

    int nSize = Marshal.SizeOf(typeof(TxDataStruct));

    Wednesday, July 25, 2018 6:42 AM
  • Hi Castorix31,

    It will be correct if there is no array in the class.

    It got wrong size if class with array property.

    It got wrong size = 24 if "Marshal.SizeOf(typeof(TxDataStruct));" is called.

    The expected value should be 680

       4 (UInt32)

    + 2 (ushort)

    + 20 (byte of array[20])

    + 20 * 32 (2-D byte array[20 *32] = 640)

    + 6 (byte of array[6])

    + 8 (byte of array[8])

    = 680

    Regards,

    E-John

    Wednesday, July 25, 2018 8:22 AM
  • But the size of each byte[] is 4 (in 32-bit)

    To get 680, you can do something like :

            [StructLayout(LayoutKind.Sequential)]
            public class TxDataStruct
            {
                public UInt32 ID;
                public ushort Number;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
                public byte[] Name;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20*32)]
                public byte[,] NameBitMap;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
                public byte[] Attribute ;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
                public byte[] Company;
            }

    • Marked as answer by E-John Thursday, July 26, 2018 3:24 AM
    Wednesday, July 25, 2018 9:33 AM
  • So you want the unmanaged size of the type? In my experience you should already know the unmanaged size and simply hard code it in the StructLayout attribute. The size won't fluctuate without recompilation in the case of your code so there is no reason to calculate it.

    You should also consider attributing your arrays so that the marshaler doesn't have to figure it out. I'm looking at a couple of your arrays and they look like ANSI strings so you should send them as such.

    //Not tested
    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Size = 680)]
    public struct TxDataStruct
    {
       public uint ID;
       public ushort Number;
    
       //If the unmanaged type is char*
       [MarshalAs(UnmanagedType.LPStr, SizeConst=20)]
       public string Name;
    
       public byte[,] NameBitMap = new byte[20, 32];
    
       //If the unmanaged type is char[]
       [MarshalAs(UnmanagedType.ByValTStr, SizeConst=6)]
       public string Attribute;
    
       //If the unmanaged type is char[] but the unmanaged code
       //is setting it
       public StringBuilder Company;
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, July 25, 2018 5:38 PM
    Moderator
  • Hi Castorix31,

    It works, the result is correct using your suggestion.

    Thanks and Best regards,

    E-John

    namespace TestSizeOf
    {
        [StructLayout(LayoutKind.Sequential)]
        public class TxDataStruct
        {
            public UInt32 ID;
            public ushort Number;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
            public byte[] Name = new byte[20];
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20 * 32)]
            public byte[,] NameBitMap = new byte[20,32];
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
            public byte[] Attribute = new byte[6];
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
            public byte[] Company = new byte[8];
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                int sizePack3 = Marshal.SizeOf(typeof(TxDataStruct));
    
                TxDataStruct tx = new TxDataStruct();
                for (int i = 0; i < tx.Name.Length; i++)
                {
                    tx.Name[i] = (byte)i;
                }
    
                int rowLength = tx.NameBitMap.GetLength(0);
                int columnLength = tx.NameBitMap.GetLength(1);
    
                Console.WriteLine("2-D array length(0) = {0}", rowLength);
                Console.WriteLine("2-D array length(1) = {0}", columnLength);
                Console.WriteLine("DataStructPack3 size got by Marshal.SizeOf() = " + sizePack3.ToString()); // 680 is expected
    
                Console.ReadKey();
            }
        }
    }


    • Edited by E-John Thursday, July 26, 2018 3:24 AM
    Thursday, July 26, 2018 3:20 AM
  • Hi CoolDadTx,

    Thanks for your reply.

    It is necessary to change

    [MarshalAs(UnmanagedType.LPStr, SizeConst = 20)]

    to

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]

    and do some modification like Castorix31's code.

    Thanks and Best regards,

    E-John

    Thursday, July 26, 2018 3:37 AM