다음을 통해 공유


방법: 특성을 사용하여 C/C++ 공용 구조체 만들기(C# 및 Visual Basic)

특성을 사용하여 구조체를 메모리에 배치하는 방법을 사용자 지정할 수 있습니다. 예를 들어, StructLayout(LayoutKind.Explicit) 및 FieldOffset 특성을 사용하여 C/C++의 공용 구조체를 만들 수 있습니다.

예제

이 코드 단편에서 TestUnion에 있는 모든 필드의 메모리 내 시작 위치는 동일합니다.

' Add an Imports statement for System.Runtime.InteropServices.

<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
    // Add a using directive for System.Runtime.InteropServices.

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

다음 예제에서는 각 필드의 시작 위치가 명시적으로 다르게 설정됩니다.

 ' Add an Imports statement for System.Runtime.InteropServices.

 <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       
    // Add a using directive for System.Runtime.InteropServices.

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

두 개의 정수 필드, 즉 i1과 i2는 lg와 동일한 메모리 위치를 공유합니다. 이러한 종류의 구조체 배치 제어는 플랫폼 호출을 사용할 때 유용합니다.

참고 항목

참조

리플렉션(C# 및 Visual Basic)

특성(C# 및 Visual Basic)

사용자 지정 특성 만들기(C# 및 Visual Basic)

리플렉션을 사용하여 특성 액세스(C# 및 Visual Basic)

System.Reflection

Attribute

개념

C# 프로그래밍 가이드

기타 리소스

Visual Basic 프로그래밍 가이드

특성을 사용하여 메타데이터 확장