AssemblyBuilder クラス
アセンブリ: mscorlib (mscorlib.dll 内)
構文
<ComVisibleAttribute(True)> _ <ClassInterfaceAttribute(ClassInterfaceType.None)> _ Public NotInheritable Class AssemblyBuilder Inherits Assembly Implements _AssemblyBuilder
[ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType.None)] public sealed class AssemblyBuilder : Assembly, _AssemblyBuilder
[ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType::None)] public ref class AssemblyBuilder sealed : public Assembly, _AssemblyBuilder
動的アセンブリは、Reflection Emit API を使用して作成されたアセンブリです。Save メソッドを使用して動的アセンブリが保存されるときに、動的モジュールがアセンブリに保存されます。実行可能ファイルを生成するには、SetEntryPoint メソッドを呼び出して、アセンブリへのエントリ ポイントであるメソッドを識別する必要があります。アセンブリは、既定ではダイナミック リンク ライブラリ (DLL) として保存されますが、SetEntryPoint を使用して、コンソール アプリケーションまたは Windows ベースのアプリケーションを生成するように要求できます。
動的アセンブリに複数の動的モジュールが含まれている場合、そのアセンブリのマニフェスト ファイル名は、DefineDynamicModule の最初の引数に指定されているモジュール名に一致する必要があります。
GetModules および GetLoadedModules などの基本クラス Assembly のメソッドの中には、AssemblyBuilder オブジェクトが呼び出されたときに正しく動作しないものがあります。定義された動的アセンブリを読み込み、読み込まれたアセンブリに対してメソッドを呼び出します。たとえば、返されたモジュール リストにリソース モジュールが含まれているようにするには、読み込まれた Assembly オブジェクトに対して GetModules を呼び出します。
KeyPair を使用した動的アセンブリへの署名は、アセンブリがディスクに保存されるまで有効ではありません。したがって、遷移動的アセンブリでは厳密な名前が機能しません。
System.Reflection.Assembly
System.Reflection.Emit.AssemblyBuilder
プラットフォーム
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
AssemblyBuilder クラス
アセンブリ: System.Web (system.web.dll 内)
構文
解説
1 つ以上のファイルをコンパイル済みのアセンブリにビルドするには、AssemblyBuilder クラスのインスタンスが BuildProvider クラスのメソッドと共に使用されます。
BuildProvider クラスはビルド機能をファイル別に定義します。AssemblyBuilder クラスは、各 BuildProvider インスタンスによって指定されたソース コードを 1 つのアセンブリに結合します。ASP.NET ビルド環境では、1 つ以上のファイルから 1 つのアセンブリを構築するときに、AssemblyBuilder オブジェクトが BuildProvider メソッドに渡されます。これにより、各 BuildProvider インスタンスは、アセンブリ全体に対してそれぞれのファイルのソース コードを指定できます。
ASP.NET ビルド環境では、プロジェクト内のファイルが必要とする言語とコンパイラは、BuildProvider.CodeCompilerType プロパティに基づいて判断されます。ASP.NET ビルド環境では、コンパイラ設定に基づいてファイルをグループ化し、同一のコンパイラを必要とするファイルからアセンブリをビルドします。
CodeDomProvider プロパティは、ASP.NET ビルド環境が各 BuildProvider 実装で指定されるソース コードをアセンブリにコンパイルするのに使用する CodeDomProvider 実装を示します。
BuildProvider オブジェクトは、AddCodeCompileUnit メソッドを使用して、CodeDOM グラフの形式でソース コードを指定します。BuildProvider オブジェクトは、CreateCodeFile メソッドを使用して、物理ファイル内に格納されたソース コードを指定します。
各 BuildProvider オブジェクトが適切な AssemblyBuilder メソッドを使用してソース コードを指定した後は、ASP.NET ビルド環境は、AssemblyBuilder クラスを使用して、収集済みのソース コードをアセンブリにコンパイルします。
簡単なビルド プロバイダを、抽象基本クラス BuildProvider から継承して実装するコード例を次に示します。ビルド プロバイダは、基本クラスのメンバである CodeCompilerType、GetGeneratedType、および GenerateCode をオーバーライドします。
GenerateCode メソッドの実装では、ビルド プロバイダは CreateCodeFile メソッドを使用して、アセンブリ コンパイル用の生成済みコードを追加します。
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 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
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); } }
System.Web.Compilation.AssemblyBuilder
プラットフォーム
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- AssemblyBuilder クラスのページへのリンク