Aracılığıyla paylaş


Nasıl yapılır: bir c/C++ Birliği (C# ve Visual Basic) özniteliklerini kullanarak oluşturma

Öznitelikleri kullanarak yapılar bellekte nasıl yerleştirilir özelleştirebilirsiniz.Örneğin, ne olarak c/C++ UNION kullanarak bilinen oluşturabilirsiniz StructLayout(LayoutKind.Explicit) ve FieldOffset öznitelikleri.

Örnek

Bu kod kesimindeki tüm alanları TestUnion bellekte aynı konumda başlatın.

<System.Runtime.InteropServices.StructLayout( 
      System.Runtime.InteropServices.LayoutKind.Explicit)> 
Structure TestUnion
    <System.Runtime.InteropServices.FieldOffset(0)> 
    Public i As Integer

    <System.Runtime.InteropServices.FieldOffset(0)> 
    Public d As Double

    <System.Runtime.InteropServices.FieldOffset(0)> 
    Public c As Char

    <System.Runtime.InteropServices.FieldOffset(0)> 
    Public b As Byte
End Structure
[System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]
struct TestUnion
{
    [System.Runtime.InteropServices.FieldOffset(0)]
    public int i;

    [System.Runtime.InteropServices.FieldOffset(0)]
    public double d;

    [System.Runtime.InteropServices.FieldOffset(0)]
    public char c;

    [System.Runtime.InteropServices.FieldOffset(0)]
    public byte b;
}

Aşağıda başka bir örnek, farklı alanları başlangıçta konumlarını açıkça belirlendiği yerlerde bulunmaktadır.

 <System.Runtime.InteropServices.StructLayout( 
      System.Runtime.InteropServices.LayoutKind.Explicit)> 
Structure TestExplicit
     <System.Runtime.InteropServices.FieldOffset(0)> 
     Public lg As Long

     <System.Runtime.InteropServices.FieldOffset(0)> 
     Public i1 As Integer

     <System.Runtime.InteropServices.FieldOffset(4)> 
     Public i2 As Integer

     <System.Runtime.InteropServices.FieldOffset(8)> 
     Public d As Double

     <System.Runtime.InteropServices.FieldOffset(12)> 
     Public c As Char

     <System.Runtime.InteropServices.FieldOffset(14)> 
     Public b As Byte
 End Structure       
[System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]
struct TestExplicit
{
    [System.Runtime.InteropServices.FieldOffset(0)]
    public long lg;

    [System.Runtime.InteropServices.FieldOffset(0)]
    public int i1;

    [System.Runtime.InteropServices.FieldOffset(4)]
    public int i2;

    [System.Runtime.InteropServices.FieldOffset(8)]
    public double d;

    [System.Runtime.InteropServices.FieldOffset(12)]
    public char c;

    [System.Runtime.InteropServices.FieldOffset(14)]
    public byte b;
}

İki tamsayı alanlarının i1 ve i2, aynı bellek konumları olarak paylaşmak lg.Bu tür bir yapı düzeni kontrolünü platform çağırma kullanılırken yararlıdır.

Ayrıca bkz.

Başvuru

Yansıtma (C# ve Visual Basic)

Öznitelikler (C# ve Visual Basic)

Özel öznitelikler (C# ve Visual Basic) oluşturma

Yansıtma (C# ve Visual Basic) kullanarak niteliklere erişme

System.Reflection

Attribute

Kavramlar

C# Programlama Kılavuzu

Diğer Kaynaklar

Visual Basic programlama Kılavuzu

Öznitelikleri kullanarak meta verileri genişletme