共用方式為


AssemblyBuilder 類別

定義

提供一個容器,讓 ASP.NET 專案中一個或多個虛擬路徑建立組裝。

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

範例

以下程式碼範例說明了一個簡單的建置提供者實作,繼承自抽象 BuildProvider 基底類別。 建置提供者會 CodeCompilerType覆蓋 、 GetGeneratedType以及 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 建置環境在從一個或多個檔案建置組合語言時,會將物件傳 AssemblyBuilder 給方法 BuildProvider ,讓每個 BuildProvider 實例都能將其檔案的原始碼貢獻到整體組合語言中。

ASP.NET 建置環境會根據該 BuildProvider.CodeCompilerType 屬性決定專案內檔案所需的語言與編譯器。 建置環境會根據檔案編譯器的設定分組,並從需要相同編譯器的檔案建立組合語言。

屬性 CodeDomProvider 表示 CodeDomProvider ASP.NET 建置環境用來從每個 BuildProvider 實作所貢獻的原始碼編譯組合語言的實作。

物件 BuildProvider 會以 AddCodeCompileUnit CodeDOM 圖形的形式貢獻原始碼。 BuildProvider物件會透過此CreateCodeFile方法貢獻儲存在實體檔案中的原始碼。

每個物件使用適當AssemblyBuilder方法貢獻原始碼後BuildProvider,ASP.NET 建置環境會使用該AssemblyBuilder類別將收集的原始碼編譯成組合語言。

屬性

名稱 Description
CodeDomProvider

負責編譯用來將原始碼編譯成組合語言。

方法

名稱 Description
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)

適用於

另請參閱