AssemblyBuilder 類別

定義

提供容器,以便在 ASP.NET 專案中從一個或多個虛擬路徑建立組件。

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

範例

下列程式碼範例說明簡單的建置提供者實作,繼承自抽象 BuildProvider 基類。 建置提供者會 CodeCompilerType 覆寫基類的 、 GetGeneratedTypeGenerateCode 成員。

在方法實作 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 建置環境會在從一或多個檔案建置元件時,將 物件傳遞 AssemblyBuilderBuildProvider 方法,讓每個 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)

適用於

另請參閱