Type.StructLayoutAttribute Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets a StructLayoutAttribute that describes the layout of the current type.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

Public Overridable ReadOnly Property StructLayoutAttribute As StructLayoutAttribute

Property Value

Type: System.Runtime.InteropServices.StructLayoutAttribute

Gets a StructLayoutAttribute that describes the gross layout features of the current type.

Exception Condition

The invoked method is not supported in the base class.

StructLayoutAttribute is not returned by the GetCustomAttributes method. Instead, use this property to get it.

The following code example first defines a class, a structure, and a structure with special layout attributes (the structures are nested within the class). The example then uses the StructLayoutAttribute property to obtain a StructLayoutAttribute for each type, and displays the properties of the attributes.

Imports System
Imports System.Runtime.InteropServices

Public Class Example
    Public Shared Sub Main()
    End Sub

    Private Shared Sub DisplayLayoutAttribute( _
        ByVal sla As StructLayoutAttribute)
        Console.WriteLine(vbCrLf & "CharSet: " & sla.CharSet.ToString() _
            & vbCrLf & "   Pack: " & sla.Pack.ToString() _
            & vbCrLf & "   Size: " & sla.Size.ToString() _
            & vbCrLf & "  Value: " & sla.Value.ToString())
    End Sub

    Public Structure Test1
        Public B1 As Byte
        Public S As Short
        Public B2 As Byte
    End Structure

    <StructLayout(LayoutKind.Explicit, Pack:=1)> _
    Public Structure Test2
        <FieldOffset(0)> Public B1 As Byte
        <FieldOffset(1)> Public S As Short
        <FieldOffset(3)> Public B2 As Byte
    End Structure
End Class

.NET Framework
Available since 2.0
Return to top