AssemblyBuilder Class

Definition

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

C#
public class AssemblyBuilder
Inheritance
AssemblyBuilder

Examples

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.

C#
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Compilation;
using System.CodeDom.Compiler;
using System.CodeDom;
using System.Security;
using System.Security.Permissions;

// Define a simple build provider implementation.
[PermissionSet(SecurityAction.Demand, Unrestricted = true)]
public class SampleBuildProvider : BuildProvider
{
    // Define an internal member for the compiler type.
    protected CompilerType _compilerType = null;

    public SampleBuildProvider()
    {
        // Set the compiler to use Visual Basic.
        _compilerType = GetDefaultCompilerTypeForLanguage("C#");
    }

    // Return the internal CompilerType member 
    // defined in this implementation.
    public override CompilerType CodeCompilerType
    {
        get { return _compilerType; }
    }

    // Define a method that returns details for the 
    // code compiler for this build provider.
    public string GetCompilerTypeDetails()
    {
        StringBuilder details = new StringBuilder("");

        if (_compilerType != null)
        {
            // Format a string that contains the code compiler
            // implementation, and various compiler details.

            details.AppendFormat("CodeDomProvider type: {0}; \n",
                _compilerType.CodeDomProviderType.ToString());
            details.AppendFormat("Compiler debug build = {0}; \n",
                _compilerType.CompilerParameters.IncludeDebugInformation.ToString());
            details.AppendFormat("Compiler warning level = {0}; \n",
                _compilerType.CompilerParameters.WarningLevel.ToString());

            if (_compilerType.CompilerParameters.CompilerOptions != null)
            {
                details.AppendFormat("Compiler options: {0}; \n",
                    _compilerType.CompilerParameters.CompilerOptions.ToString());
            }
        }
        return details.ToString();
    }

    // Define the build provider implementation of the GenerateCode method.
    public override void GenerateCode(AssemblyBuilder assemBuilder)
    {
        // Generate a code compile unit, and add it to
        // the assembly builder.

        TextWriter tw = assemBuilder.CreateCodeFile(this);
        if (tw != null)
        {
            try
            {
                // Generate the code compile unit from the virtual path.
                CodeCompileUnit compileUnit = SampleClassGenerator.BuildCompileUnitFromPath(VirtualPath);

                // Generate the source for the code compile unit, 
                // and write it to a file specified by the assembly builder.
                CodeDomProvider provider = assemBuilder.CodeDomProvider;
                provider.CreateGenerator().GenerateCodeFromCompileUnit(compileUnit, tw, null);
            }
            finally
            {
                tw.Close();
            }
        }
    }

    public override System.Type GetGeneratedType(CompilerResults results)
    {
        string typeName = SampleClassGenerator.TypeName;

        return results.CompiledAssembly.GetType(typeName);
    }
}

Remarks

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.

Properties

CodeDomProvider

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

Methods

AddAssemblyReference(Assembly)

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

AddCodeCompileUnit(BuildProvider, CodeCompileUnit)

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

CreateCodeFile(BuildProvider)

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

CreateEmbeddedResource(BuildProvider, String)

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

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GenerateTypeFactory(String)

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

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetTempFilePhysicalPath(String)

Generates a temporary file path.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

Proizvod Verzije
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also