BuildProvider.GenerateCode(AssemblyBuilder) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
產生建置提供者虛擬路徑的原始碼,並將原始碼加入指定的組合語言建置器。
public:
virtual void GenerateCode(System::Web::Compilation::AssemblyBuilder ^ assemblyBuilder);
public virtual void GenerateCode(System.Web.Compilation.AssemblyBuilder assemblyBuilder);
abstract member GenerateCode : System.Web.Compilation.AssemblyBuilder -> unit
override this.GenerateCode : System.Web.Compilation.AssemblyBuilder -> unit
Public Overridable Sub GenerateCode (assemblyBuilder As AssemblyBuilder)
參數
- assemblyBuilder
- AssemblyBuilder
就是參考建置提供者產生的原始碼的組合語言建置器。
範例
以下程式碼範例說明了一個簡單的建置提供者實作,繼承自抽象 BuildProvider 基底類別。 建置提供者會 CodeCompilerType覆蓋 、 GetGeneratedType以及 GenerateCode 基類成員。
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
備註
若要實作一個產生自訂檔案類型原始碼的建置提供者,請從該 BuildProvider 類別衍生出一個類別,並覆寫 GenerateCode 產生支援檔案類型原始碼的方法。
通常,建置提供者的 GenerateCode 實作會 VirtualPath 讀取屬性、解析內容,並將產生的原始碼加入指定的 AssemblyBuilder 物件。 你可以以物件或 CodeDOM 圖形的形式,貢獻原始碼到組合語言 TextWriter 。 若要透過物件將原始碼加入組合語言 TextWriter ,請使用該 CreateCodeFile 方法。 若要將 CodeDOM 圖中的原始碼加入組合語言,請使用該 AddCodeCompileUnit 方法。
如果建置提供者產生特定程式語言的原始碼,你必須覆寫該 CodeCompilerType 屬性以回傳 CompilerType 該程式語言的物件。