Lire en anglais

Partager via


Type.StructLayoutAttribute Propriété

Définition

Obtient un StructLayoutAttribute qui décrit la disposition du type actuel.

C#
public virtual System.Runtime.InteropServices.StructLayoutAttribute? StructLayoutAttribute { get; }
C#
public virtual System.Runtime.InteropServices.StructLayoutAttribute StructLayoutAttribute { get; }

Valeur de propriété

Obtient un StructLayoutAttribute qui décrit les fonctionnalités de disposition brutes du type actuel.

Exceptions

La méthode appelée n’est pas prise en charge dans la classe de base.

Exemples

L’exemple de code suivant définit d’abord une classe, une structure et une structure avec des attributs de disposition spéciaux (les structures sont imbriquées dans la classe). L’exemple utilise ensuite la StructLayoutAttribute propriété pour obtenir un StructLayoutAttribute pour chaque type et affiche les propriétés des attributs.

C#
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;
    }
}

Remarques

StructLayoutAttribute n’est pas retourné par la GetCustomAttributes méthode . Utilisez plutôt cette propriété pour l’obtenir.

S’applique à

Produit Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Voir aussi