AssemblyBuilder 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供一个容器,用以从 ASP.NET 项目内的一个或多个虚拟路径生成程序集。
public ref class AssemblyBuilderpublic class AssemblyBuildertype AssemblyBuilder = classPublic Class AssemblyBuilder- 继承
- 
				AssemblyBuilder
示例
下面的代码示例演示了从抽象 BuildProvider 基类继承的简单生成提供程序实现。 生成提供程序将CodeCompilerTypeGetGeneratedType替代基类的成员和GenerateCode成员。
在方法实现中 GenerateCode ,生成提供程序使用 CreateCodeFile 该方法为程序集编译添加生成的代码。 该示例不包括类的 SampleClassGenerator 实现。 有关详细信息,请参阅 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
注解
类 AssemblyBuilder 的实例与类方法一起使用 BuildProvider ,用于将一个或多个文件生成到已编译的程序集中。
该 BuildProvider 类定义单个文件的生成功能,该 AssemblyBuilder 类将每个 BuildProvider 实例提供的源代码合并到单个程序集中。 ASP.NET 生成环境从一个或多个文件生成程序集时,将对象BuildProvider传递给AssemblyBuilder方法,以便每个BuildProvider实例都可以将其文件的源代码贡献给整个程序集。
ASP.NET 生成环境根据BuildProvider.CodeCompilerType属性确定项目中文件所需的语言和编译器。 生成环境基于其编译器设置对文件进行分组,并从需要同一编译器的文件生成程序集。
该CodeDomProvider属性指示 CodeDomProvider ASP.NET 生成环境用于从每个BuildProvider实现提供的源代码编译程序集的实现。
对象 BuildProvider 使用 AddCodeCompileUnit 该方法以 CodeDOM 图形的形式提供源代码。 对象 BuildProvider 使用 CreateCodeFile 该方法贡献存储在物理文件中的源代码。
每个BuildProvider对象使用适当的AssemblyBuilder方法提供源代码后,ASP.NET 生成环境使用该AssemblyBuilder类将收集的源代码编译到程序集中。
属性
| CodeDomProvider | 获取用于将源代码生成为程序集的编译器。 | 
方法
| AddAssemblyReference(Assembly) | 添加为文件生成的源代码所引用的程序集。 | 
| AddCodeCompileUnit(BuildProvider, CodeCompileUnit) | 以 CodeDOM 图的形式为程序集添加源代码。 | 
| CreateCodeFile(BuildProvider) | 允许版本提供程序创建临时源文件,并在程序集编译中包含此源文件。 | 
| CreateEmbeddedResource(BuildProvider, String) | 允许版本提供程序创建要包含在程序集编译中的资源文件。 | 
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GenerateTypeFactory(String) | 将类型的快速对象工厂模板插入到编译后的程序集中。 | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetTempFilePhysicalPath(String) | 生成临时文件路径。 | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) |