AssemblyBuilder 类

定义

提供一个容器,用以从 ASP.NET 项目内的一个或多个虚拟路径生成程序集。Provides a container for building an assembly from one or more virtual paths within an ASP.NET project.

public ref class AssemblyBuilder
public class AssemblyBuilder
type AssemblyBuilder = class
Public Class AssemblyBuilder
继承
AssemblyBuilder

示例

下面的代码示例演示了一个简单的生成提供程序实现, 该BuildProvider实现继承自抽象基类。The following code example illustrates a simple build provider implementation, inheriting from the abstract BuildProvider base class. 生成提供程序会重CodeCompilerTypeGetGeneratedType基类的GenerateCode 、和成员。The build provider overrides the CodeCompilerType, GetGeneratedType, and GenerateCode members of the base class.

在方法实现中, 生成提供程序CreateCodeFile使用方法为程序集编译添加生成的代码。 GenerateCodeIn the GenerateCode method implementation, the build provider adds the generated code for the assembly compilation using the CreateCodeFile method. 该示例不包括SampleClassGenerator类的实现。The example does not include the implementation of the SampleClassGenerator class. 有关详细信息,请参阅 CodeCompileUnitFor more information, see CodeCompileUnit.

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);
    }
}
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
        Get
            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}; ", _
                _compilerType.CodeDomProviderType.ToString())
            details.AppendFormat("Compiler debug build = {0}; ", _
                _compilerType.CompilerParameters.IncludeDebugInformation.ToString())
            details.AppendFormat("Compiler warning level = {0}; ", _
                _compilerType.CompilerParameters.WarningLevel.ToString())

            If Not _compilerType.CompilerParameters.CompilerOptions Is Nothing Then
                details.AppendFormat("Compiler options: {0}; ", _
                    _compilerType.CompilerParameters.CompilerOptions.ToString())
            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
            Try
                ' Generate the code compile unit from the virtual path.
                Dim compileUnit As CodeCompileUnit = _
                        SampleClassGenerator.BuildCompileUnitFromPath(VirtualPath)

                ' 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)
            Finally
                tw.Close()
            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

注解

类的实例与BuildProvider类方法一起使用, 以将一个或多个文件生成到已编译的程序集中。 AssemblyBuilderInstances of the AssemblyBuilder class are used with BuildProvider class methods to build one or more files into a compiled assembly.

类定义各个文件的生成功能, 类将AssemblyBuilder每个BuildProvider实例提供的源代码组合到单个程序集中。 BuildProviderThe 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. 当从一个或多个AssemblyBuilder文件生成程序BuildProvider集时, ASP.NET 生成环境会将对象传递给方法, 以便BuildProvider每个实例都可以为其文件提供整个程序集的源代码。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.

ASP.NET 生成环境根据BuildProvider.CodeCompilerType属性确定项目中的文件所需的语言和编译器。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.

属性指示 ASP.NET 生成环境使用的BuildProvider 实现,该实现由每个实现提供的源代码编译程序集。CodeDomProvider CodeDomProviderThe 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.

BuildProvider 对象AddCodeCompileUnit使用方法以 CodeDOM 图的形式提供源代码。A BuildProvider object contributes source code in the form of a CodeDOM graph using the AddCodeCompileUnit method. BuildProvider 对象CreateCodeFile使用方法来提供存储在物理文件中的源代码。A BuildProvider object contributes source code stored in a physical file using the CreateCodeFile method.

每个BuildProvider对象使用适当AssemblyBuilder的方法提供源代码后, AssemblyBuilder ASP.NET 生成环境将使用类将收集的源代码编译为程序集。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.

属性

CodeDomProvider

获取用于将源代码生成为程序集的编译器。Gets the compiler used to build source code into an assembly.

方法

AddAssemblyReference(Assembly)

添加为文件生成的源代码所引用的程序集。Adds an assembly that is referenced by source code generated for a file.

AddCodeCompileUnit(BuildProvider, CodeCompileUnit)

以 CodeDOM 图的形式为程序集添加源代码。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.

(继承自 Object)
GenerateTypeFactory(String)

将类型的快速对象工厂模板插入到编译后的程序集中。Inserts a fast object factory template for a type into the compiled assembly.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetTempFilePhysicalPath(String)

生成临时文件路径。Generates a temporary file path.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅