Freigeben über


How to: Create a C/C++ Union by Using Attributes (C# and Visual Basic)

By using attributes you can customize how structs are laid out in memory. For example, you can create what is known as a union in C/C++ by using the StructLayout(LayoutKind.Explicit) and FieldOffset attributes.

Example

In this code segment, all of the fields of TestUnion start at the same location in memory.

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

The following is another example where fields start at different explicitly set locations.

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

The two integer fields, i1 and i2, share the same memory locations as lg. This sort of control over struct layout is useful when using platform invocation.

See Also

Reference

Reflection (C# and Visual Basic)

Attributes (C# and Visual Basic)

Creating Custom Attributes (C# and Visual Basic)

Accessing Attributes by Using Reflection (C# and Visual Basic)

System.Reflection

Attribute

Concepts

C# Programming Guide

Other Resources

Visual Basic Programming Guide

Extending Metadata Using Attributes