Export (0) Print
Expand All

EnumBuilder.SetCustomAttribute Method (CustomAttributeBuilder)

Sets a custom attribute using a custom attribute builder.

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

Public Sub SetCustomAttribute ( _
	customBuilder As CustomAttributeBuilder _


Type: System.Reflection.Emit.CustomAttributeBuilder
An instance of a helper class to define the custom attribute.


con is Nothing.

The following code sample illustrates the use of SetCustomAttribute in the context of EnumBuilder, passing a CustomAttributeBuilder.

Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit

<AttributeUsage(AttributeTargets.All, AllowMultiple := False)>  _
Public Class MyAttribute
   Inherits Attribute
   Public myBoolValue As Boolean

   Public Sub New(myBool As Boolean)
      Me.myBoolValue = myBool
   End Sub 'New
End Class 'MyAttribute

Class MyApplication
   Private Shared myEnumBuilder As EnumBuilder

   Public Shared Sub Main()
         Dim myAttributesArray As Object() = myEnumBuilder.GetCustomAttributes(True)

         ' Read the attributes and display them on the console.
         Console.WriteLine("Custom attribute contains: ")
         Dim index As Integer
         For index = 0 To myAttributesArray.Length - 1
            If TypeOf myAttributesArray(index) Is MyAttribute Then
               Console.WriteLine("myBoolValue: " + CType(myAttributesArray(index), MyAttribute). _
            End If
         Next index
      Catch e As Exception
         Console.WriteLine("The following exception is raised:" + e.Message)
      End Try
   End Sub 'Main

   Private Shared Sub CreateCallee(domain As AppDomain)
      Dim myAssemblyName As New AssemblyName()
      ' Create a name for the assembly.
      myAssemblyName.Name = "EmittedAssembly"

      ' Create the dynamic assembly.
      Dim myAssemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(myAssemblyName, _

      Dim myType As Type = GetType(MyAttribute)
      Dim myInfo As ConstructorInfo = myType.GetConstructor(New Type() {GetType(Boolean)})

      ' Create a dynamic module.
      Dim myModuleBuilder As ModuleBuilder = myAssemblyBuilder.DefineDynamicModule("EmittedModule")

      ' Create a dynamic Enum.
      myEnumBuilder = myModuleBuilder.DefineEnum("MyNamespace.MyEnum", TypeAttributes.Public, _

      Dim myFieldBuilder1 As FieldBuilder = myEnumBuilder.DefineLiteral("FieldOne", 1)
      Dim myFieldBuilder2 As FieldBuilder = myEnumBuilder.DefineLiteral("FieldTwo", 2)

      myEnumBuilder.SetCustomAttribute(myInfo, New Byte() {1, 0, 1})
   End Sub 'CreateCallee 
End Class 'MyApplication

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

© 2014 Microsoft