VBCodeProvider 類別

定義

提供存取 Visual Basic 程式碼產生器和程式碼編譯器的執行個體。

public ref class VBCodeProvider : System::CodeDom::Compiler::CodeDomProvider
public class VBCodeProvider : System.CodeDom.Compiler.CodeDomProvider
type VBCodeProvider = class
    inherit CodeDomProvider
Public Class VBCodeProvider
Inherits CodeDomProvider
繼承

範例

下列範例會使用 C# 或 Visual Basic 程式代碼提供者來編譯原始程序檔。 此範例會檢查輸入擴展名,並使用對應的 CSharpCodeProviderVBCodeProvider 進行編譯。 輸入檔會編譯成可執行檔,而且任何編譯錯誤都會顯示在控制台中。

public static bool CompileExecutable(String sourceName)
{
    FileInfo sourceFile = new FileInfo(sourceName);
    CodeDomProvider provider = null;
    bool compileOk = false;

    // Select the code provider based on the input file extension.
    if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".CS")
    {
        provider = CodeDomProvider.CreateProvider("CSharp");
    }
    else if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".VB")
    {
        provider = CodeDomProvider.CreateProvider("VisualBasic");
    }
    else
    {
        Console.WriteLine("Source file must have a .cs or .vb extension");
    }

    if (provider != null)
    {

        // Format the executable file name.
        // Build the output assembly path using the current directory
        // and <source>_cs.exe or <source>_vb.exe.

        String exeName = String.Format(@"{0}\{1}.exe",
            System.Environment.CurrentDirectory,
            sourceFile.Name.Replace(".", "_"));

        CompilerParameters cp = new CompilerParameters();

        // Generate an executable instead of
        // a class library.
        cp.GenerateExecutable = true;

        // Specify the assembly file name to generate.
        cp.OutputAssembly = exeName;

        // Save the assembly as a physical file.
        cp.GenerateInMemory = false;

        // Set whether to treat all warnings as errors.
        cp.TreatWarningsAsErrors = false;

        // Invoke compilation of the source file.
        CompilerResults cr = provider.CompileAssemblyFromFile(cp,
            sourceName);

        if(cr.Errors.Count > 0)
        {
            // Display compilation errors.
            Console.WriteLine("Errors building {0} into {1}",
                sourceName, cr.PathToAssembly);
            foreach(CompilerError ce in cr.Errors)
            {
                Console.WriteLine("  {0}", ce.ToString());
                Console.WriteLine();
            }
        }
        else
        {
            // Display a successful compilation message.
            Console.WriteLine("Source {0} built into {1} successfully.",
                sourceName, cr.PathToAssembly);
        }

        // Return the results of the compilation.
        if (cr.Errors.Count > 0)
        {
            compileOk = false;
        }
        else
        {
            compileOk = true;
        }
    }
    return compileOk;
}
       Public Shared Function CompileExecutable(sourceName As String) As Boolean
           Dim sourceFile As FileInfo = New FileInfo(sourceName)
           Dim provider As CodeDomProvider = Nothing
           Dim compileOk As Boolean = False

           ' Select the code provider based on the input file extension.
           If sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) = ".CS"

               provider = CodeDomProvider.CreateProvider("CSharp")

           ElseIf sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) = ".VB"

               provider = CodeDomProvider.CreateProvider("VisualBasic")

           Else
               Console.WriteLine("Source file must have a .cs or .vb extension")
           End If

           If Not provider Is Nothing

               ' Format the executable file name.
               ' Build the output assembly path using the current directory
               ' and <source>_cs.exe or <source>_vb.exe.

               Dim exeName As String = String.Format("{0}\{1}.exe", _
                   System.Environment.CurrentDirectory, _
                   sourceFile.Name.Replace(".", "_"))

               Dim cp As CompilerParameters = new CompilerParameters()

               ' Generate an executable instead of 
               ' a class library.
               cp.GenerateExecutable = True

               ' Specify the assembly file name to generate.
               cp.OutputAssembly = exeName
   
               ' Save the assembly as a physical file.
               cp.GenerateInMemory = False
   
               ' Set whether to treat all warnings as errors.
               cp.TreatWarningsAsErrors = False

               ' Invoke compilation of the source file.
               Dim cr As CompilerResults = provider.CompileAssemblyFromFile(cp, _
                   sourceName)
   
               If cr.Errors.Count > 0
                   ' Display compilation errors.
                   Console.WriteLine("Errors building {0} into {1}", _
                       sourceName, cr.PathToAssembly)

                   Dim ce As CompilerError
                   For Each ce In cr.Errors
                       Console.WriteLine("  {0}", ce.ToString())
                       Console.WriteLine()
                   Next ce
               Else
                   ' Display a successful compilation message.
                   Console.WriteLine("Source {0} built into {1} successfully.", _
                       sourceName, cr.PathToAssembly)
               End If
             
               ' Return the results of the compilation.
               If cr.Errors.Count > 0
                   compileOk = False
               Else 
                   compileOk = True
               End If
           End If
           return compileOk

       End Function

備註

這個類別提供可用來擷取Visual Basic ICodeGenerator 實例和 ICodeCompiler 實作的方法。

注意

這個類別包含套用至所有成員之類別層級的連結需求和繼承需求。 SecurityException當立即呼叫端或衍生類別沒有完全信任權限時,就會擲回 。

建構函式

VBCodeProvider()

初始化 VBCodeProvider 類別的新執行個體。

VBCodeProvider(IDictionary<String,String>)

使用指定的提供者選項,初始化 VBCodeProvider 類別的新執行個體。

屬性

CanRaiseEvents

取得值,指出元件是否能引發事件。

(繼承來源 Component)
Container

取得包含 IContainerComponent

(繼承來源 Component)
DesignMode

取得值,指出 Component 目前是否處於設計模式。

(繼承來源 Component)
Events

取得附加在這個 Component 上的事件處理常式清單。

(繼承來源 Component)
FileExtension

取得建立原始程式碼檔案時要使用的副檔名。

LanguageOptions

取得語言功能識別項。

Site

取得或設定 ComponentISite

(繼承來源 Component)

方法

CompileAssemblyFromDom(CompilerParameters, CodeCompileUnit[])

使用指定的編譯器設定,根據 System.CodeDom 物件之指定陣列所包含的 CodeCompileUnit 樹狀結構,編譯一個組件。

(繼承來源 CodeDomProvider)
CompileAssemblyFromFile(CompilerParameters, String[])

使用指定的編譯器設定,在指定檔案所包含的原始程式碼中,編譯組件。

(繼承來源 CodeDomProvider)
CompileAssemblyFromSource(CompilerParameters, String[])

使用指定的編譯器設定,在含有原始程式碼的指定陣列中,編譯一個組件。

(繼承來源 CodeDomProvider)
CreateCompiler()
已淘汰.
已淘汰.

取得 Visual Basic 程式碼編譯器的執行個體。

CreateEscapedIdentifier(String)

為指定值建立逸出識別項。

(繼承來源 CodeDomProvider)
CreateGenerator()
已淘汰.
已淘汰.

取得 Visual Basic 程式碼產生器的執行個體。

CreateGenerator(String)

在衍生類別中覆寫時,請使用指定的輸出檔名建立新的程式碼產生器。

(繼承來源 CodeDomProvider)
CreateGenerator(TextWriter)

在衍生類別中覆寫時,請使用指定的輸出 TextWriter 建立新的程式碼產生器。

(繼承來源 CodeDomProvider)
CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
CreateParser()
已淘汰.
已淘汰.

在衍生類別中覆寫時,請建立新的程式碼剖析器。

(繼承來源 CodeDomProvider)
CreateValidIdentifier(String)

為指定值建立有效的識別項。

(繼承來源 CodeDomProvider)
Dispose()

釋放 Component 所使用的所有資源。

(繼承來源 Component)
Dispose(Boolean)

釋放 Component 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

(繼承來源 Component)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GenerateCodeFromCompileUnit(CodeCompileUnit, TextWriter, CodeGeneratorOptions)

產生指定程式碼文件物件模型 (CodeDOM) 編譯單位 (Compilation Unit) 的程式碼,並使用指定的選項將其傳送至指定的文字寫入器。

(繼承來源 CodeDomProvider)
GenerateCodeFromExpression(CodeExpression, TextWriter, CodeGeneratorOptions)

產生指定程式碼文件物件模型 (CodeDOM) 運算式的程式碼,並使用指定的選項將其傳送至指定的文字寫入器。

(繼承來源 CodeDomProvider)
GenerateCodeFromMember(CodeTypeMember, TextWriter, CodeGeneratorOptions)

使用指定的文字寫入器與程式碼產生器選項,產生指定類別成員的程式碼。

GenerateCodeFromMember(CodeTypeMember, TextWriter, CodeGeneratorOptions)

產生指定程式碼文件物件模型 (CodeDOM) 成員宣告的程式碼,並使用指定的選項將其傳送至指定的文字寫入器。

(繼承來源 CodeDomProvider)
GenerateCodeFromNamespace(CodeNamespace, TextWriter, CodeGeneratorOptions)

產生指定程式碼文件物件模型 (CodeDOM) 命名空間的程式碼,並使用指定選項將其傳送至指定的文字寫入器。

(繼承來源 CodeDomProvider)
GenerateCodeFromStatement(CodeStatement, TextWriter, CodeGeneratorOptions)

產生指定程式碼文件物件模型 (CodeDOM) 陳述式的程式碼,並使用指定選項將其傳送至指定的文字寫入器。

(繼承來源 CodeDomProvider)
GenerateCodeFromType(CodeTypeDeclaration, TextWriter, CodeGeneratorOptions)

產生指定程式碼文件物件模型 (CodeDOM) 型別宣告的程式碼,並使用指定選項將其傳送至指定的文字寫入器。

(繼承來源 CodeDomProvider)
GetConverter(Type)

取得物件指定類型的 TypeConverter

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。

(繼承來源 Component)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
GetTypeOutput(CodeTypeReference)

取得由指定的 CodeTypeReference 所指示的型別。

(繼承來源 CodeDomProvider)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
IsValidIdentifier(String)

傳回值,指出指定的值是否為目前語言的有效識別項。

(繼承來源 CodeDomProvider)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
Parse(TextReader)

將指定文字資料流中所讀取的程式碼編譯成 CodeCompileUnit

(繼承來源 CodeDomProvider)
Supports(GeneratorSupport)

傳回值,指出是否提供指定程式碼產生支援。

(繼承來源 CodeDomProvider)
ToString()

傳回任何包含 Component 名稱的 String。 不應覆寫此方法。

(繼承來源 Component)

事件

Disposed

Dispose() 方法的呼叫處置元件時,就會發生。

(繼承來源 Component)

適用於

另請參閱