How to control the physical layout of the data fields in the .NET Framework 2.0

INTRODUCTION

You can use the System.Runtime.InteropServices namespace and the StructLayoutAttribute class to control the physical layout of the data fields in the Microsoft .NET Framework 2.0. The target objects for these data fields are classes and structures. Typically, the common language runtime controls the physical layout of the data fields of a class or of a structure in managed memory. When you pass managed objects as arguments to unmanaged code, the system creates unmanaged representations of the objects. The StructLayoutAttribute class can control these unmanaged representations. Such control is necessary if the unmanaged code expects a specific layout or packing size. A StructLayoutAttribute constructor that uses the LayoutKind value as its parameter is used in the following example.
StructLayoutAttribute (System.Runtime.InteropServices.LayoutKind)

This code constructs and initializes a new instance of the StructLayoutAttribute class that uses the specified LayoutKind value.

The LayoutKind value Sequential is used to force the members of the structure to be laid out sequentially in the order that they appear in. The System.Runtime.InteropServices.StructLayoutAttribute.Pack field determines the memory alignment of data fields of a target object.

The value of the System.Runtime.InteropServices.StructLayoutAttribute.Pack field must be 0, 1, 2, 4, 8, 16, 32, 64, or 128. A value of 0 indicates that the packing alignment is set to the default for the current operating system. The default value is defined according to the implementation.

More Information

The Visual Basic sample code in this section uses the sizeof operator to calculate the size of the following structure.
Structure TestDByte
Dim a As Double
Dim b As Byte
End Structure
When you run the following sample program, the program calculates the size of this structure as 16.
using System;
using System.Runtime.InteropServices;

struct TestDByte
{
public double a;
public byte b;
}

class Test
{
unsafe static void Main()
{
TestDByte tdb;
Console.WriteLine( "Size: " + sizeof(tdb) );

}
}
If you do not use the StructLayoutAttribute.Pack field, the program shows the size of the structure as 16.


The following example reduces the structure size by using the StructLayoutAttribute.Pack field.
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack=1)]
struct TestDByte
{
public double a;
public byte b;
}

class Test
{
unsafe static void Main()
{
TestDByte tdb;
Console.WriteLine( "Size: " + sizeof(tdb) );

}
}
The following example reduces the structure size to 9.
[StructLayout(LayoutKind.Sequential, Pack=1)]
The value 9 is equal to the sum of all the fields in the structure.

References

For more information about how to use the StructLayoutattribute class from the .NET Framework, visit the following Microsoft Web site:
Propriedades

ID do Artigo: 922785 - Última Revisão: 24/03/2009 - Revisão: 1

Comentários