AssemblyBuilder.CreateCodeFile(BuildProvider) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
允許建置提供者建立暫存的原始碼檔案,並將該原始碼檔案包含在組合語言編譯中。
public:
System::IO::TextWriter ^ CreateCodeFile(System::Web::Compilation::BuildProvider ^ buildProvider);
public System.IO.TextWriter CreateCodeFile(System.Web.Compilation.BuildProvider buildProvider);
member this.CreateCodeFile : System.Web.Compilation.BuildProvider -> System.IO.TextWriter
Public Function CreateCodeFile (buildProvider As BuildProvider) As TextWriter
參數
- buildProvider
- BuildProvider
建置提供者產生程式碼原始碼檔案。
傳回
一個可以用來寫原始碼到暫存檔案的 open TextWriter 。
範例
以下程式碼範例說明了一個簡單的建置提供者實作,繼承自抽象 BuildProvider 基底類別。 建置提供者會 CodeCompilerType覆蓋 、 GetGeneratedType以及 GenerateCode 基類成員。
在方法實作中 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
備註
實作在 BuildProvider 產生虛擬路徑的原始檔案時會呼叫此 CreateCodeFile 方法。 加入的 CreateCodeFile 原始碼包含在組合語言編譯中。
通常,建置提供者 GenerateCode 方法的實作會讀取該 VirtualPath 屬性,解析內容,然後將產生的原始碼加入指定的 AssemblyBuilder 物件。 建置提供者會使用此 CreateCodeFile 方法將原始碼以實體檔案形式加入組合語言。 或者,建置提供者也可以使用此 AddCodeCompileUnit 方法將原始碼以 CodeDOM 圖的形式加入組合語言。
呼叫 CreateCodeFile後,建置提供者會利用回傳 TextWriter 的物件寫入原始檔案內容。 寫入原始碼檔案後, BuildProvider 物件必須使用該 Close 方法關閉物件 TextWriter 並釋放相關的系統資源。