Aracılığıyla paylaş


Type.StructLayoutAttribute Özellik

Tanım

Geçerli türün düzenini açıklayan bir StructLayoutAttribute alır.

public:
 virtual property System::Runtime::InteropServices::StructLayoutAttribute ^ StructLayoutAttribute { System::Runtime::InteropServices::StructLayoutAttribute ^ get(); };
public virtual System.Runtime.InteropServices.StructLayoutAttribute? StructLayoutAttribute { get; }
public virtual System.Runtime.InteropServices.StructLayoutAttribute StructLayoutAttribute { get; }
member this.StructLayoutAttribute : System.Runtime.InteropServices.StructLayoutAttribute
Public Overridable ReadOnly Property StructLayoutAttribute As StructLayoutAttribute

Özellik Değeri

Geçerli türün brüt düzen özelliklerini açıklayan bir StructLayoutAttribute alır.

Özel durumlar

Çağrılan yöntem temel sınıfta desteklenmez.

Örnekler

Aşağıdaki kod örneği önce bir sınıfı, bir yapıyı ve özel düzen özniteliklerine sahip bir yapıyı tanımlar (yapılar sınıfın içinde iç içe yerleştirilmiştir). Örnek daha sonra her tür için bir StructLayoutAttribute almak için özelliğini kullanır StructLayoutAttribute ve özniteliklerin özelliklerini görüntüler.

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;
    }
}
open System.Runtime.InteropServices

type Example = class end

[<Struct>]
type Test1 = 
    val B1 : byte
    val S : int16
    val B2 : byte

[<Struct; StructLayout(LayoutKind.Explicit, Pack=1)>] 
type Test2 =
    [<FieldOffset 0>] val B1 : byte
    [<FieldOffset 1>] val S : int16
    [<FieldOffset 3>] val B2 : byte

let displayLayoutAttribute (sla: StructLayoutAttribute) =
    printfn $"\nCharSet: {sla}\n   Pack: {sla.Pack}\n   Size: {sla.Size}\n  Value: {sla.Value}"
displayLayoutAttribute typeof<Example>.StructLayoutAttribute
displayLayoutAttribute typeof<Test1>.StructLayoutAttribute
displayLayoutAttribute typeof<Test2>.StructLayoutAttribute
Imports System.Runtime.InteropServices

Public Class Example
    Public Shared Sub Main()
        DisplayLayoutAttribute(GetType(Example).StructLayoutAttribute)
        DisplayLayoutAttribute(GetType(Test1).StructLayoutAttribute)
        DisplayLayoutAttribute(GetType(Test2).StructLayoutAttribute)
    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

Açıklamalar

StructLayoutAttribute yöntemi tarafından GetCustomAttributes döndürülmedi. Bunun yerine, almak için bu özelliği kullanın.

Şunlara uygulanır

Ayrıca bkz.