Type.StructLayoutAttribute 属性

定义

获取一个描述当前类型的布局的 StructLayoutAttribute

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

属性值

获取一个描述当前类型的大致布局特性的 StructLayoutAttribute

例外

基类不支持调用的方法。

示例

下面的代码示例首先定义类、结构和具有特殊布局属性的结构, (结构嵌套在类) 中。 然后,该示例使用 StructLayoutAttribute 属性获取 StructLayoutAttribute 每种类型的 ,并显示属性的属性。

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

注解

StructLayoutAttribute 方法不返回 GetCustomAttributes 。 请改用此属性获取它。

适用于

产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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

另请参阅