AssemblyBuilder Class

Defines and represents a dynamic assembly.


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

<ClassInterfaceAttribute(ClassInterfaceType.None)> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class AssemblyBuilder _
	Inherits Assembly

The AssemblyBuilder type exposes the following members.

Public propertyCodeBaseGets the location of the assembly as specified originally, for example, in an AssemblyName object. (Inherited from Assembly.)
Public propertyEntryPointGets the entry point of this assembly. (Overrides Assembly.EntryPoint.)
Public propertyFullNameGets the display name of the current dynamic assembly. (Overrides Assembly.FullName.)
Public propertyImageRuntimeVersionGets the version of the common language runtime that will be saved in the file containing the manifest. (Overrides Assembly.ImageRuntimeVersion.)
Public propertyIsDynamicGets a value that indicates that the current assembly is a dynamic assembly. (Overrides Assembly.IsDynamic.)
Public propertyLocationGets the path or UNC location of the loaded file that contains the manifest. (Inherited from Assembly.)
Public propertyManifestModuleGets the module in the current AssemblyBuilder that contains the assembly manifest. (Overrides Assembly.ManifestModule.)

Public methodCreateInstanceLocates the specified type from this assembly and creates an instance of it using the system activator, using case-sensitive search. (Inherited from Assembly.)
Public methodDefineDynamicModule(String)Defines a named transient dynamic module in this assembly.
Public methodDefineDynamicModule(String, Boolean)Defines a named transient dynamic module in this assembly and specifies whether symbol information should be emitted.
Public methodEqualsReturns a value that indicates whether this instance is equal to the specified object. (Overrides Object.Equals(Object).)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetCustomAttributes(Boolean)Returns all the custom attributes that have been applied to the current AssemblyBuilder. (Overrides Assembly.GetCustomAttributes(Boolean).)
Public methodGetCustomAttributes(Type, Boolean)Returns all the custom attributes that have been applied to the current AssemblyBuilder, and that derive from a specified attribute type. (Overrides Assembly.GetCustomAttributes(Type, Boolean).)
Public methodGetDynamicModuleReturns the dynamic module that has the specified name.
Public methodGetExportedTypesGets the exported types that are defined in this assembly. (Overrides Assembly.GetExportedTypes.)
Public methodGetFileSecurity Critical. Gets a FileStream for the specified file in the file table of the manifest of this assembly. (Inherited from Assembly.)
Public methodGetFilesSecurity Critical. Gets the files in the file table of an assembly manifest, specifying whether to include resource modules. (Inherited from Assembly.)
Public methodGetHashCodeReturns the hash code for this instance. (Overrides Object.GetHashCode.)
Public methodGetManifestResourceNamesLoads the names of the manifest resources in this assembly. (Overrides Assembly.GetManifestResourceNames.)
Public methodGetManifestResourceStream(String)Loads the specified manifest resource from this assembly. (Overrides Assembly.GetManifestResourceStream(String).)
Public methodGetManifestResourceStream(Type, String)Loads the specified manifest resource, scoped by the namespace of the specified type, from this assembly. (Overrides Assembly.GetManifestResourceStream(Type, String).)
Public methodGetModulesGets all the modules that are part of this assembly. (Inherited from Assembly.)
Public methodGetNameSecurity Critical. Gets the AssemblyName that was specified when the current dynamic assembly was created, and sets the code base as specified. (Overrides Assembly.GetName(Boolean).)
Public methodGetSatelliteAssembly(CultureInfo)Gets the satellite assembly for the specified culture. (Overrides Assembly.GetSatelliteAssembly(CultureInfo).)
Public methodGetSatelliteAssembly(CultureInfo, Version)Gets the specified version of the satellite assembly for the specified culture. (Overrides Assembly.GetSatelliteAssembly(CultureInfo, Version).)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetType(String)Gets the Type object with the specified name in the assembly instance. (Inherited from Assembly.)
Public methodGetType(String, Boolean)Gets the Type object with the specified name in the assembly instance and optionally throws an exception if the type is not found. (Inherited from Assembly.)
Public methodGetTypesGets the types defined in this assembly. (Inherited from Assembly.)
Public methodIsDefinedReturns a value that indicates whether one or more instances of the specified attribute type is applied to this member. (Overrides Assembly.IsDefined(Type, Boolean).)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSetCustomAttributeSets a custom attribute on this assembly by using a custom attribute builder.
Public methodToStringReturns the full name of the assembly, also known as the display name. (Inherited from Assembly.)

A dynamic assembly is an assembly that is created using the reflection emit APIs. In Silverlight, all dynamic assemblies are transient, and therefore cannot be saved. A dynamic assembly contains only one dynamic module.

To get an AssemblyBuilder object, use the AppDomain.DefineDynamicAssembly method.

The following example shows how to define a dynamic assembly with one module. The module in the example assembly contains one type, MyDynamicType, which has a private field, a property that gets and sets the private field, constructors that initialize the private field, and a method that multiplies a user-supplied number by the private field value and returns the result.

Imports System.Reflection
Imports System.Reflection.Emit

Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' In this version of the runtime, an assembly consists of one 
      ' module which contains zero or more types. This example 
      ' creates an assembly containing one public type named
      ' "MyDynamicType". The type has a private field, a property 
      ' that gets and sets the private field, constructors that 
      ' initialize the private field, and a method that multiplies
      ' a user-supplied number by the private field value and returns 
      ' the result. The code might look like this in Visual Basic:
      'Public Class MyDynamicType
      '    Private m_number As Integer
      '    Public Sub New()
      '        Me.New(42)
      '    End Sub
      '    Public Sub New(ByVal initNumber As Integer)
      '        m_number = initNumber
      '    End Sub
      '    Public Property Number As Integer
      '        Get
      '            Return m_number
      '        End Get
      '        Set
      '            m_Number = Value
      '        End Set
      '    End Property
      '    Public Function MyMethod(ByVal multiplier As Integer) As Integer
      '        Return m_Number * multiplier
      '    End Function

      Dim aName As New AssemblyName("DynamicAssemblyExample")
      Dim ab As AssemblyBuilder = _
          AppDomain.CurrentDomain.DefineDynamicAssembly( _
              aName, _

      ' Create the module.
      Dim mb As ModuleBuilder = ab.DefineDynamicModule(aName.Name)

      Dim tb As TypeBuilder = _
          mb.DefineType("MyDynamicType", TypeAttributes.Public)

      ' Add a private field of type Integer (Int32).
      Dim fbNumber As FieldBuilder = tb.DefineField( _
          "m_number", _
          GetType(Integer), _

      ' Define a constructor that takes an integer argument and 
      ' stores it in the private field. 
      Dim parameterTypes() As Type = {GetType(Integer)}
      Dim ctor1 As ConstructorBuilder = _
          tb.DefineConstructor( _
              MethodAttributes.Public, _
              CallingConventions.Standard, _

      Dim ctor1IL As ILGenerator = ctor1.GetILGenerator()
      ' For a constructor, argument zero is a reference to the new
      ' instance. Push it on the stack before calling the base
      ' class constructor. Specify the default constructor of the 
      ' base class (System.Object) by passing an empty array of 
      ' types (Type.EmptyTypes) to GetConstructor.
      ctor1IL.Emit(OpCodes.Call, _
      ' Push the instance on the stack before pushing the argument
      ' that is to be assigned to the private field m_number.
      ctor1IL.Emit(OpCodes.Stfld, fbNumber)

      ' Define a default constructor that supplies a default value
      ' for the private field. For parameter types, pass the empty
      ' array of types or pass Nothing.
      Dim ctor0 As ConstructorBuilder = tb.DefineConstructor( _
          MethodAttributes.Public, _
          CallingConventions.Standard, _

      Dim ctor0IL As ILGenerator = ctor0.GetILGenerator()
      ' For a constructor, argument zero is a reference to the new
      ' instance. Push it on the stack before pushing the default
      ' value on the stack, then call constructor ctor1.
      ctor0IL.Emit(OpCodes.Ldc_I4_S, 42)
      ctor0IL.Emit(OpCodes.Call, ctor1)

      ' Define a property named Number that gets and sets the private 
      ' field.
      ' The last argument of DefineProperty is Nothing, because the
      ' property has no parameters. (If you don't specify Nothing, you must
      ' specify an array of Type objects. For a parameterless property,
      ' use the built-in array with no elements: Type.EmptyTypes)
      Dim pbNumber As PropertyBuilder = tb.DefineProperty( _
          "Number", _
          PropertyAttributes.HasDefault, _
          GetType(Integer), _

      ' The property Set and property Get methods require a special
      ' set of attributes.
      Dim getSetAttr As MethodAttributes = _
          MethodAttributes.Public Or MethodAttributes.SpecialName _
              Or MethodAttributes.HideBySig

      ' Define the "get" accessor method for Number. The method returns
      ' an integer and has no arguments. (Note that Nothing could be 
      ' used instead of Types.EmptyTypes)
      Dim mbNumberGetAccessor As MethodBuilder = tb.DefineMethod( _
          "get_Number", _
          getSetAttr, _
          GetType(Integer), _

      Dim numberGetIL As ILGenerator = mbNumberGetAccessor.GetILGenerator()
      ' For an instance property, argument zero is the instance. Load the 
      ' instance, then load the private field and return, leaving the
      ' field value on the stack.
      numberGetIL.Emit(OpCodes.Ldfld, fbNumber)

      ' Define the "set" accessor method for Number, which has no return
      ' type and takes one argument of type Integer (Int32).
      Dim mbNumberSetAccessor As MethodBuilder = _
          tb.DefineMethod( _
              "set_Number", _
              getSetAttr, _
              Nothing, _
              New Type() {GetType(Integer)})

      Dim numberSetIL As ILGenerator = mbNumberSetAccessor.GetILGenerator()
      ' Load the instance and then the numeric argument, then store the
      ' argument in the field.
      numberSetIL.Emit(OpCodes.Stfld, fbNumber)

      ' Last, map the "get" and "set" accessor methods to the 
      ' PropertyBuilder. The property is now complete. 

      ' Define a method that accepts an integer argument and returns
      ' the product of that integer and the private field m_number. This
      ' time, the array of parameter types is created on the fly.
      Dim meth As MethodBuilder = tb.DefineMethod( _
          "MyMethod", _
          MethodAttributes.Public, _
          GetType(Integer), _
          New Type() {GetType(Integer)})

      Dim methIL As ILGenerator = meth.GetILGenerator()
      ' To retrieve the private instance field, load the instance it
      ' belongs to (argument zero). After loading the field, load the 
      ' argument one and then multiply. Return from the method with 
      ' the return value (the product of the two numbers) on the 
      ' execution stack.
      methIL.Emit(OpCodes.Ldfld, fbNumber)

      ' Finish the type.
      Dim t As Type = tb.CreateType()

      ' The code can be executed immediately. Start by getting reflection
      ' objects for the method and the property.
      Dim mi As MethodInfo = t.GetMethod("MyMethod")
      Dim pi As PropertyInfo = t.GetProperty("Number")

      ' Create an instance of MyDynamicType using the default 
      ' constructor. 
      Dim o1 As Object = Activator.CreateInstance(t)

      ' Display the value of the property, then change it to 127 and 
      ' display it again. Use Nothing to indicate that the property
      ' has no index.
      outputBlock.Text += String.Format("o1.Number: {0}" & vbCrLf, _
          pi.GetValue(o1, Nothing)) 
      pi.SetValue(o1, 127, Nothing)
      outputBlock.Text += String.Format("o1.Number: {0}" & vbCrLf, _
          pi.GetValue(o1, Nothing)) 

      ' Call MyMethod, passing 22, and display the return value, 22
      ' times 127. Arguments must be passed as an array, even when
      ' there is only one.
      Dim arguments() As Object = {22}
      outputBlock.Text &= String.Format("o1.MyMethod(22): {0}" & vbCrLf, _
          mi.Invoke(o1, arguments))

      ' Create an instance of MyDynamicType using the constructor
      ' that specifies m_Number. The constructor is identified by
      ' matching the types in the argument array. In this case, 
      ' the argument array is created on the fly. Display the 
      ' property value.
      Dim o2 As Object = Activator.CreateInstance(t, _
          New Object() {5280})
      outputBlock.Text += String.Format("o2.Number: {0}" & vbCrLf, _
          pi.GetValue(o2, Nothing))

   End Sub
End Class

' This code produces the following output:
'o1.Number: 42
'o1.Number: 127
'o1.MyMethod(22): 2794
'o2.Number: 5280


Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions