CompilerType 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表 ASP.NET 建置環境中用於從虛擬路徑產生與編譯原始碼的編譯器設定。 無法繼承這個類別。
public ref class CompilerType sealed
public sealed class CompilerType
type CompilerType = class
Public NotInheritable Class CompilerType
- 繼承
-
CompilerType
範例
以下程式碼範例說明了一個簡單的建置提供者實作,繼承自抽象 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()
{
// 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
備註
利用 CodeCompilerType 實 BuildProvider 作的特性檢視該建置提供者用來從虛擬路徑產生與編譯原始碼的設定。
ASP.NET 建置環境利用 BuildProvider 物件產生專案內自訂檔案類型的原始碼。 衍生自 的 BuildProvider 類別提供原始碼檔案、網頁、資源及其他自訂項目的建置細節。 當建置提供者需要特定的程式語言時,會覆寫該 CodeCompilerType 屬性並呼叫該 GetDefaultCompilerType 方法來回傳 CompilerType 支援程式語言的物件。
若要在建置提供者實作中設定物件, CompilerType 請使用該 GetDefaultCompilerType 方法或該 GetDefaultCompilerTypeForLanguage 方法。
此 CodeDomProviderType 屬性指定用於產生與編譯建置提供者原始碼的 CodeDomProvider 實作。 此 CompilerParameters 屬性定義了將原始碼編譯成組合語言的設定。
屬性
| 名稱 | Description |
|---|---|
| CodeDomProviderType |
為配置CodeDomProvider的實作獲得 aType。 |
| CompilerParameters |
取得將原始碼編譯成組合語言的設定與選項。 |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
判斷指定物件是否代表與目前實例 CompilerType相同的程式碼提供者與編譯器設定。 |
| GetHashCode() |
傳回這個實例的哈希碼。 |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |