BuildProvider 類別

定義

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

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

範例

下列程式碼範例說明繼承自抽象 BuildProvider 基類的簡單組建提供者實作。 組建提供者會 CodeCompilerType 覆寫基類的 、 GetGeneratedTypeGenerateCode 成員。 此範例不包含 類別的實作 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 實例,在應用程式中建置檔案。 類別 VirtualPathBuildProvider 屬性工作表示要建置之檔案的路徑。 應用程式內每個檔案的副檔名都會對應至對應的組建提供者。 ASP.NET 建置環境會根據副檔名初始化 BuildProvider 每個檔案的實例,並使用 BuildProvider 方法來產生檔案的原始程式碼。 ASP.NET 建置環境會根據慣用的編譯器語言和檔案內容,將物件傳遞 AssemblyBuilderBuildProvider 從一或多個檔案建置元件時的方法,讓 BuildProvider 實例可以將其檔案的原始程式碼提供給整體元件。

若要定義 ASP.NET 應用程式中檔案類型的自訂建置動作,您必須從 BuildProvider 衍生類別衍生類別、實作衍生類別內的成員來建置檔案類型,以及為應用程式組態檔內的對應副檔名設定組建提供者。

元素 add 會指定支援檔案的副檔名,以及組建提供者是否支援程式碼檔案、Web 檔案、資源檔或所有檔案。 type使用 屬性來指定組建提供者實作的完整型別名稱。 使用 類別 BuildProviderAppliesToAttribute 可指定組建提供者是否適用于 App_Code 目錄中的檔案、Web 內容目錄中的檔案、全域或本機資源,或套用至所有檔案。 extension使用 屬性來指定用來識別 類別支援的檔案的 BuildProvider 副檔名。 使用 類別 BuildProviderCollection 來檢查組態檔中的組建提供者。 如需設定組建提供者的詳細資訊,請參閱buildProviders 元素以進行編譯 (ASP.NET 設定架構)

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

若要實作產生 Web 內容的原始程式碼的組建提供者,請從 BuildProvider 衍生類別,並覆寫 GetGeneratedType 方法以傳回 Type 所產生類別的 BuildProviderGenerateCode覆寫 方法,以針對支援的檔案所提供的類型產生原始程式碼。

注意

將自訂 BuildProvider 類別新增至Web.config檔案可在 ASP.NET 網站中運作,但無法在 ASP.NET Web 應用程式專案中運作。 在 Web 應用程式專案中,類別所產生的 BuildProvider 程式碼不能包含在應用程式中。 如需詳細資訊,請參閱ASP.NET Web 應用程式Project先行編譯概觀

建構函式

BuildProvider()

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

屬性

CodeCompilerType

表示組建提供者用來產生自訂檔案類型之原始程式碼的編譯器型別。

ReferencedAssemblies

表示使用組建提供者產生之原始碼編譯的組件。

VirtualPath

表示此組建提供者實作要建立之檔案。

VirtualPathDependencies

表示必須在組建提供者產生程式碼之前先建立的虛擬路徑集合。

方法

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)

適用於

另請參閱