Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Type.StructLayoutAttribute Property

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

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

public virtual StructLayoutAttribute StructLayoutAttribute { get; }
/** @property */
public StructLayoutAttribute get_StructLayoutAttribute ()

public function get StructLayoutAttribute () : StructLayoutAttribute

Not applicable.

Property Value

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

Exception typeCondition

NotSupportedException

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.

using System;
using System.Runtime.InteropServices;

public class Example
{
    public static void Main()
    {
        DisplayLayoutAttribute(typeof(Example).StructLayoutAttribute);
        DisplayLayoutAttribute(typeof(Test1).StructLayoutAttribute);
        DisplayLayoutAttribute(typeof(Test2).StructLayoutAttribute);
    }

    private static void DisplayLayoutAttribute(StructLayoutAttribute sla)
    {
        Console.WriteLine("\r\nCharSet: "+sla.CharSet.ToString()+"\r\n   Pack: "+sla.Pack.ToString()+"\r\n   Size: "+sla.Size.ToString()+"\r\n  Value: "+sla.Value.ToString());
    }
    public struct Test1
    {
        public byte B1;
        public short S;
        public byte B2;
    }
    [StructLayout(LayoutKind.Explicit, Pack=1)] public struct Test2
    {
        [FieldOffset(0)] public byte B1;
        [FieldOffset(1)] public short S;
        [FieldOffset(3)] public byte B2;
    }
}

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft