AssemblyBuilder Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Provides a container for building an assembly from one or more virtual paths within an ASP.NET project.

Namespace:   System.Web.Compilation
Assembly:  System.Web (in System.Web.dll)


Public Class AssemblyBuilder


Gets the compiler used to build source code into an assembly.


Adds an assembly that is referenced by source code generated for a file.

System_CAPS_pubmethodAddCodeCompileUnit(BuildProvider, CodeCompileUnit)

Adds source code for the assembly in the form of a CodeDOM graph.


Allows a build provider to create a temporary source file, and include the source file in the assembly compilation.

System_CAPS_pubmethodCreateEmbeddedResource(BuildProvider, String)

Allows a build provider to create a resource file to include in the assembly compilation.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Inserts a fast object factory template for a type into the compiled assembly.


Serves as the default hash function. (Inherited from Object.)


Generates a temporary file path.


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

Instances of the AssemblyBuilder class are used with BuildProvider class methods to build one or more files into a compiled assembly.

The BuildProvider class defines build functionality for individual files, and the AssemblyBuilder class combines the source code contributed by each BuildProvider instance into a single assembly. The ASP.NET build environment passes an AssemblyBuilder object to the BuildProvider methods when building an assembly from one or more files, so that each BuildProvider instance can contribute source code for its file to the overall assembly.

The ASP.NET build environment determines the language and compiler required by files within the project, based on the BuildProvider.CodeCompilerType property. The build environment groups files based on their compiler settings and builds an assembly from files that require the same compiler.

The CodeDomProvider property indicates the CodeDomProvider implementation that the ASP.NET build environment uses to compile an assembly from the source code contributed by each BuildProvider implementation.

A BuildProvider object contributes source code in the form of a CodeDOM graph using the AddCodeCompileUnit method. A BuildProvider object contributes source code stored in a physical file using the CreateCodeFile method.

After each BuildProvider object contributes source code using the appropriate AssemblyBuilder methods, the ASP.NET build environment uses the AssemblyBuilder class to compile the collected source code into an assembly.

The following code example illustrates a simple build provider implementation, inheriting from the abstract BuildProvider base class. The build provider overrides the CodeCompilerType, GetGeneratedType, and GenerateCode members of the base class.

In the GenerateCode method implementation, the build provider adds the generated code for the assembly compilation using the CreateCodeFile method. The example does not include the implementation of the SampleClassGenerator class. For more information, see CodeCompileUnit.

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.IO
Imports System.Text
Imports System.Web
Imports System.Web.Compilation
Imports System.CodeDom.Compiler
Imports System.CodeDom
Imports System.Security
Imports System.Security.Permissions

<PermissionSet(SecurityAction.Demand, Unrestricted := true)> _
Public Class SampleBuildProvider
    Inherits BuildProvider

    Protected _compilerType As CompilerType = Nothing

    Public Sub New()
        _compilerType = GetDefaultCompilerType()
    End Sub

    ' Return the internal CompilerType member 
    ' defined in this implementation.
    Public Overrides ReadOnly Property CodeCompilerType() As CompilerType
            CodeCompilerType = _compilerType
        End Get
    End Property

    ' Define a method that returns details for the 
    ' code compiler for this build provider.
    Public Function GetCompilerTypeDetails() As String
        Dim details As StringBuilder = New StringBuilder("")

        If Not _compilerType Is Nothing Then
            ' Format a string that contains the code compiler
            ' implementation, and various compiler details.

            details.AppendFormat("CodeDomProvider type: {0}; ", _
            details.AppendFormat("Compiler debug build = {0}; ", _
            details.AppendFormat("Compiler warning level = {0}; ", _

            If Not _compilerType.CompilerParameters.CompilerOptions Is Nothing Then
                details.AppendFormat("Compiler options: {0}; ", _
            End If
        End If
        Return details.ToString()
    End Function

    ' Define the build provider implementation of the GenerateCode method.
    Public Overrides Sub GenerateCode(ByVal assemBuilder As AssemblyBuilder)
        ' Generate a code compile unit, and add it to
        ' the assembly builder.

        Dim tw As TextWriter = assemBuilder.CreateCodeFile(Me)
        If Not tw Is Nothing Then
                ' Generate the code compile unit from the virtual path.
                Dim compileUnit As CodeCompileUnit = _

                ' Generate the source for the code compile unit, 
                ' and write it to a file specified by the assembly builder.
                Dim provider As CodeDomProvider = assemBuilder.CodeDomProvider
                provider.CreateGenerator().GenerateCodeFromCompileUnit(compileUnit, tw, Nothing)
            End Try

        End If
    End Sub

    Public Overrides Function GetGeneratedType(ByVal results As CompilerResults) As System.Type
        Dim typeName As String = SampleClassGenerator.TypeName

        Return results.CompiledAssembly.GetType(typeName)
    End Function

End Class

.NET Framework
Available since 2.0

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

Return to top