C# Type StructLayoutAttribute

Description

Type StructLayoutAttribute gets a StructLayoutAttribute that describes the layout of the current type.

Syntax

Type.StructLayoutAttribute has the following syntax.


public virtual StructLayoutAttribute StructLayoutAttribute { get; }

Example

The following code example first defines a class, a structure, and a structure with special layout attributes.


//ww w.j  a va2s.  c  o m
using System;
using System.Runtime.InteropServices;

public class Example
{
    public static void Main()
    {
        DisplayLayoutAttribute(typeof(Example).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());
    }
    [StructLayout(LayoutKind.Explicit, Pack=1)] public struct Test2
    {
        [FieldOffset(0)] public byte B1;
        [FieldOffset(1)] public short S;
        [FieldOffset(3)] public byte B2;
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version