BuildProvider クラス
アセンブリ: System.Web (system.web.dll 内)


BuildProvider オブジェクトは、ASP.NET ビルド環境で 1 つのアプリケーション内に複数のファイルの種類でソース コードを生成するために使用されます。BuildProvider の派生クラスでは、ファイル、Web ページ、リソース、およびその他のカスタム アイテム用のソース コードを主に提供します。
通常、BuildProvider クラスのインスタンスを直接作成することはありません。その代わり、BuildProvider から派生したクラスを実装し、ASP.NET ビルド環境で使用するために BuildProvider 実装を設定します。
BuildProvider クラスのインスタンスを AssemblyBuilder オブジェクトと共に使用して、1 つ以上のファイルをコンパイル済みのアセンブリにビルドします。BuildProvider インスタンスは、ファイルごとのソース コードを適切な言語で生成します。AssemblyBuilder オブジェクトは、各 BuildProvider インスタンスによって指定されたソース コードを単一のアセンブリに結合します。
ASP.NET ビルド環境では、BuildProvider クラスのインスタンスを使用してアプリケーション内のファイルをビルドします。BuildProvider クラスの VirtualPath プロパティは、ビルドされるファイルへのパスを示します。アプリケーション内の各ファイルの拡張子は、対応するビルド プロバイダにマップされます。ASP.NET ビルド環境では、各ファイルの BuildProvider インスタンスをファイル拡張子に基づいて初期化し、BuildProvider メソッドを使用してファイルごとにソース コードを生成します。ASP.NET ビルド環境では、1 つ以上のファイルから 1 つのアセンブリを構築するとき、AssemblyBuilder オブジェクトが、優先されるコンパイラ言語とファイル コンテキストに基づいて BuildProvider メソッドに渡されます。これにより、BuildProvider インスタンスはアセンブリ全体に対し、それぞれのファイルのソース コードを指定できます。
ASP.NET アプリケーション内にファイルの種類ごとにカスタム ビルド アクションを定義するには、BuildProvider からクラスを派生して、この派生クラス内にファイルの種類をビルドするためのメンバを実装してから、アプリケーション構成ファイル内のファイル拡張子に対応させるためのビルド プロバイダを設定する必要があります。
add 要素は、サポートするファイルのファイル拡張子と、ビルド プロバイダがコード ファイル、Web ファイル、リソース ファイルまたはすべてのファイルをサポートしているかどうかを指定します。ビルド プロバイダ実装の完全修飾型名を指定するには、type 属性を使用します。ビルド プロバイダの適用対象を、App_Code ディレクトリ内のファイル、Web コンテンツ ディレクトリ内のファイル、グローバルまたはローカル リソース、またはすべてのファイルのいずれにするかを指定するには、appliesTo 属性を使用します。BuildProvider クラスがサポートするファイルの識別に使用するファイル拡張子を指定するには、extension 属性を使用します。構成ファイル内のビルド プロバイダを調べるには、BuildProviderCollection クラスを使用します。ビルド プロバイダの構成の詳細については、「compilation の buildProviders 要素 (ASP.NET 設定スキーマ)」を参照してください。
独自のファイルの種類でソース コードを生成するビルド プロバイダを実装するには、BuildProvider からクラスを派生し、GenerateCode メソッドをオーバーライドすることで、目的のファイルの種類をサポートするソース コードを生成します。生成されたソース コードは、CodeDOM グラフの形式で、または物理ソース コード ファイルを表すコンテンツとして、AssemblyBuilder オブジェクトに追加されます。ビルド プロバイダで独自のプログラミング言語が必要な場合は、CodeCompilerType プロパティをオーバーライドして、そのプログラミング言語をサポートする CompilerType オブジェクトを返すようにします。ビルド プロバイダが独自のプログラミング言語を必要としない場合は、CodeCompilerType プロパティをオーバーライドせずに基本クラス実装を使用します。これにより、Visual Basic または C# などの .NET Framework 言語を使用できるビルド プロバイダが指定されます。
Web コンテンツのソース コードを生成するビルド プロバイダを実装するには、BuildProvider からクラスを派生し、BuildProvider によって生成されるクラスの Type を返すための GetGeneratedType メソッドをオーバーライドします。GenerateCode メソッドをオーバーライドすることで、サポート対象ファイルによって提供される型のソース コードを生成します。

簡単なビルド プロバイダを、抽象基本クラス BuildProvider から継承して実装するコード例を次に示します。ビルド プロバイダは、基本クラスのメンバである CodeCompilerType、GetGeneratedType、および GenerateCode をオーバーライドします。
Imports Microsoft.VisualBasic Imports System 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
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); } }

System.Web.Compilation.BuildProvider


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


BuildProvider クラス
アセンブリ: System.Web (system.web.dll 内)


System.Configuration.ConfigurationElement
System.Web.Configuration.BuildProvider


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- BuildProvider クラスのページへのリンク