共用方式為


BuildProvider 類別

定義

定義一組在 ASP.NET 建置環境中產生原始碼的屬性與方法。 這個類別是抽象的。

public ref class BuildProvider abstract
public abstract class BuildProvider
type BuildProvider = class
Public MustInherit Class BuildProvider
繼承
BuildProvider
衍生

範例

以下程式碼範例說明了一個簡單的建置提供者實作,繼承自抽象 BuildProvider 基底類別。 建置提供者會 CodeCompilerType覆蓋 、 GetGeneratedType以及 GenerateCode 基類成員。 範例中未包含該 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()
    {
        _compilerType = GetDefaultCompilerTypeForLanguage("C#");
    }

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

    // 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.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 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.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

備註

ASP.NET 建置環境利用 BuildProvider 物件來產生應用程式內不同檔案類型的原始碼。 衍生自的 BuildProvider 類別主要提供檔案、網頁、資源及其他自訂項目的原始碼。

通常,你不會直接建立該 BuildProvider 類別的實例。 相反地,你實作一個從 衍生的 BuildProvider類別,並設定 BuildProvider 該實作用於 ASP.NET 建置環境。

該類別的實例 BuildProvider 與物件一起使用 AssemblyBuilder ,將一個或多個檔案建置成已編譯的組合語言。 實 BuildProvider 例會為個別檔案產生適當語言的原始碼,物件 AssemblyBuilder 則將每個 BuildProvider 實例貢獻的原始碼合併成一個組合語言。

ASP.NET 建置環境利用類別的 BuildProvider 實例在應用程式內建置檔案。 VirtualPath類別BuildProvider的屬性表示要建置檔案的路徑。 應用程式中每個檔案的副檔名會對應到對應的建置提供者。 ASP.NET 建置環境會根據檔案副檔名初始化每個檔案的 BuildProvider 實例,並利用這些 BuildProvider 方法產生檔案的原始碼。 ASP.NET 建置環境會根據偏好的編譯器語言及檔案上下文將物件傳遞 AssemblyBuilderBuildProvider 方法,從一個或多個檔案建構組合語言,讓實 BuildProvider 例能將其檔案的原始碼貢獻到整體組合語言中。

若要在 ASP.NET 應用程式中定義檔案類型的自訂建置動作,您必須從衍生類別中推導出一個類別 BuildProvider,並在該衍生類別中實現成員以建置該檔案類型,並在應用程式設定檔中設定對應的副檔名的建置提供者。

add 元素指定支援檔案的副檔名,以及建置提供者是否支援程式碼檔案、Web 檔案、資源檔案或全部檔案。 使用屬性 type 來指定建置提供者實作的完整限定型別名稱。 使用該 BuildProviderAppliesToAttribute 類別來指定建置提供者是否適用於App_Code目錄中的檔案、網頁內容目錄中的檔案、全域或本地資源,或是所有檔案。 使用 extension 屬性指定用來識別類別所支援檔案 BuildProvider 的副檔名。 使用該 BuildProviderCollection 類別來檢視設定檔中的建置提供者。 欲了解更多關於配置建置提供者的資訊,請參閱 buildProviders 元素以進行編譯(ASP.NET 設定結構)。

要實作一個能產生自訂檔案類型原始碼的建置提供者,請從 衍生出一個類別 BuildProvider,並覆寫 GenerateCode 該方法以產生支援檔案類型的原始碼。 產生的原始碼會以 CodeDOM 圖的形式加入 AssemblyBuilder 物件,或作為代表實體原始碼檔案的內容。 若建置提供者需要特定程式語言,則覆寫該 CodeCompilerType 屬性以回傳 CompilerType 支援程式語言的物件。 如果建置提供者不需要特定的程式語言,則不要覆寫該 CodeCompilerType 屬性;使用基底類別實作,該實作表示建置提供者可以使用任何 .NET Framework 語言,例如 Visual Basic 或 C#。

要實作一個能產生網頁內容原始碼的建置提供者,請從中 BuildProvider 衍生一個類別,並覆寫 GetGeneratedType 該方法以回傳 Type 由 所產生 BuildProvider的類別。 覆蓋 GenerateCode 產生支援檔案所提供類型原始碼的方法。

備註

在 Web.config 檔案中新增自訂 BuildProvider 類別可在 ASP.NET 網站運作,但在 ASP.NET 網頁應用程式專案中則無法使用。 在網頁應用程式專案中,類別 BuildProvider 產生的程式碼無法包含在應用程式中。 欲了解更多資訊,請參閱 ASP.NET 網頁應用程式專案預編譯概述

建構函式

名稱 Description
BuildProvider()

初始化 BuildProvider 類別的新執行個體。

屬性

名稱 Description
CodeCompilerType

代表建置提供者用來產生自訂檔案類型原始碼的編譯器類型。

ReferencedAssemblies

代表要編譯的組件,並由建置提供者產生的原始碼。

VirtualPath

代表此建置提供者實作將建立的檔案。

VirtualPathDependencies

代表一組必須在建置提供者產生程式碼前建構的虛擬路徑集合。

方法

名稱 Description
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GenerateCode(AssemblyBuilder)

產生建置提供者虛擬路徑的原始碼,並將原始碼加入指定的組合語言建置器。

GetCodeCompileUnit(IDictionary)

代表產生的 CodeDOM 圖形容器。

GetCustomString(CompilerResults)

產生一個字串,要在編譯後的組裝中持久化。

GetDefaultCompilerType()

回傳應用程式中預設語言的編譯器設定。

GetDefaultCompilerTypeForLanguage(String)

會根據指定語言回傳編譯器設定,針對建置提供者。

GetGeneratedType(CompilerResults)

回傳由建置提供者從虛擬路徑產生的型別。

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetResultFlags(CompilerResults)

回傳一個值,表示建立虛擬路徑時所需的行動。

GetType()

取得目前實例的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
OpenReader()

開啟一個文字閱讀器,從目前建置提供者物件的虛擬路徑讀取。

OpenReader(String)

開啟文字閱讀器以讀取指定的虛擬路徑。

OpenStream()

開啟串流以讀取目前建置提供者物件的虛擬路徑。

OpenStream(String)

開啟一條串流以讀取指定的虛擬路徑。

ProcessCompileErrors(CompilerResults)

當在衍生類別中覆寫時,能讓你檢視編譯器的錯誤訊息,進而修改它們以提供更多資訊。

RegisterBuildProvider(String, Type)

註冊一個建置提供者。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於

另請參閱