VBCodeProvider 類別

定義

提供存取 Visual Basic 程式碼產生器和程式碼編譯器的執行個體。Provides access to instances of the Visual Basic code generator and code compiler.

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 程式碼提供者來編譯來源檔案。The following example uses either the C# or Visual Basic code provider to compile a source file. 此範例會檢查輸入檔延伸模組, 並使用CSharpCodeProvider對應VBCodeProvider的或進行編譯。The example checks the input file extension and uses the corresponding CSharpCodeProvider or VBCodeProvider for compilation. 輸入檔會編譯成可執行檔, 並在主控台中顯示任何編譯錯誤。The input file is compiled into an executable file, and any compilation errors are displayed to the console.

       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 ICodeGeneratorICodeCompiler實作為實例的方法。This class provides methods that can be used to retrieve instances of the Visual Basic ICodeGenerator and ICodeCompiler implementations.

注意

此類別包含套用至所有成員之類別層級的連結要求和繼承要求。This class contains a link demand and an inheritance demand at the class level that applies to all members. 當立即呼叫端或衍生類別沒有完全信任許可權時,就會擲回。SecurityExceptionA SecurityException is thrown when either the immediate caller or the derived class does not have full-trust permission.

建構函式

VBCodeProvider()

初始化 VBCodeProvider 類別的新執行個體。Initializes a new instance of the VBCodeProvider class.

VBCodeProvider(IDictionary<String,String>)

使用指定的提供者選項,初始化 VBCodeProvider 類別的新執行個體。Initializes a new instance of the VBCodeProvider class by using the specified provider options.

屬性

CanRaiseEvents

取得值,指出元件是否能引發事件。Gets a value indicating whether the component can raise an event.

(繼承來源 Component)
Container

取得包含 IContainerComponentGets the IContainer that contains the Component.

(繼承來源 Component)
DesignMode

取得值,指出 Component 目前是否處於設計模式。Gets a value that indicates whether the Component is currently in design mode.

(繼承來源 Component)
Events

取得附加在這個 Component 上的事件處理常式清單。Gets the list of event handlers that are attached to this Component.

(繼承來源 Component)
FileExtension

取得建立原始程式碼檔案時要使用的副檔名。Gets the file name extension to use when creating source code files.

LanguageOptions

取得語言功能識別項。Gets a language features identifier.

Site

取得或設定 ComponentISiteGets or sets the ISite of the Component.

(繼承來源 Component)

方法

CompileAssemblyFromDom(CompilerParameters, CodeCompileUnit[])

使用指定的編譯器設定,根據 System.CodeDom 物件之指定陣列所包含的 CodeCompileUnit 樹狀結構,編譯一個組件。Compiles an assembly based on the System.CodeDom trees contained in the specified array of CodeCompileUnit objects, using the specified compiler settings.

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

使用指定的編譯器設定,在指定檔案所包含的原始程式碼中,編譯組件。Compiles an assembly from the source code contained in the specified files, using the specified compiler settings.

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

使用指定的編譯器設定,在含有原始程式碼的指定陣列中,編譯一個組件。Compiles an assembly from the specified array of strings containing source code, using the specified compiler settings.

(繼承來源 CodeDomProvider)
CreateCompiler()

取得 Visual Basic 程式碼編譯器的執行個體。Gets an instance of the Visual Basic code compiler.

CreateEscapedIdentifier(String)

為指定值建立逸出識別項。Creates an escaped identifier for the specified value.

(繼承來源 CodeDomProvider)
CreateGenerator()

取得 Visual Basic 程式碼產生器的執行個體。Gets an instance of the Visual Basic code generator.

CreateGenerator(String)

在衍生類別中覆寫時,請使用指定的輸出檔名建立新的程式碼產生器。When overridden in a derived class, creates a new code generator using the specified file name for output.

(繼承來源 CodeDomProvider)
CreateGenerator(TextWriter)

在衍生類別中覆寫時,請使用指定的輸出 TextWriter 建立新的程式碼產生器。When overridden in a derived class, creates a new code generator using the specified TextWriter for output.

(繼承來源 CodeDomProvider)
CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(繼承來源 MarshalByRefObject)
CreateParser()

在衍生類別中覆寫時,請建立新的程式碼剖析器。When overridden in a derived class, creates a new code parser.

(繼承來源 CodeDomProvider)
CreateValidIdentifier(String)

為指定值建立有效的識別項。Creates a valid identifier for the specified value.

(繼承來源 CodeDomProvider)
Dispose()

釋放 Component 所使用的所有資源。Releases all resources used by the Component.

(繼承來源 Component)
Dispose(Boolean)

釋放 Component 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the Component and optionally releases the managed resources.

(繼承來源 Component)
Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

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

產生指定程式碼文件物件模型 (CodeDOM) 編譯單位 (Compilation Unit) 的程式碼,並使用指定的選項將其傳送至指定的文字寫入器。Generates code for the specified Code Document Object Model (CodeDOM) compilation unit and sends it to the specified text writer, using the specified options.

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

產生指定程式碼文件物件模型 (CodeDOM) 運算式的程式碼,並使用指定的選項將其傳送至指定的文字寫入器。Generates code for the specified Code Document Object Model (CodeDOM) expression and sends it to the specified text writer, using the specified options.

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

使用指定的文字寫入器與程式碼產生器選項,產生指定類別成員的程式碼。Generates code for the specified class member using the specified text writer and code generator options.

GenerateCodeFromNamespace(CodeNamespace, TextWriter, CodeGeneratorOptions)

產生指定程式碼文件物件模型 (CodeDOM) 命名空間的程式碼,並使用指定選項將其傳送至指定的文字寫入器。Generates code for the specified Code Document Object Model (CodeDOM) namespace and sends it to the specified text writer, using the specified options.

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

產生指定程式碼文件物件模型 (CodeDOM) 陳述式的程式碼,並使用指定選項將其傳送至指定的文字寫入器。Generates code for the specified Code Document Object Model (CodeDOM) statement and sends it to the specified text writer, using the specified options.

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

產生指定程式碼文件物件模型 (CodeDOM) 型別宣告的程式碼,並使用指定選項將其傳送至指定的文字寫入器。Generates code for the specified Code Document Object Model (CodeDOM) type declaration and sends it to the specified text writer, using the specified options.

(繼承來源 CodeDomProvider)
GetConverter(Type)

取得物件指定類型的 TypeConverterGets a TypeConverter for the specified type of object.

GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetLifetimeService()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。Returns an object that represents a service provided by the Component or by its Container.

(繼承來源 Component)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
GetTypeOutput(CodeTypeReference)

取得由指定的 CodeTypeReference 所指示的型別。Gets the type indicated by the specified CodeTypeReference.

(繼承來源 CodeDomProvider)
InitializeLifetimeService()

取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
IsValidIdentifier(String)

傳回值,指出指定的值是否為目前語言的有效識別項。Returns a value that indicates whether the specified value is a valid identifier for the current language.

(繼承來源 CodeDomProvider)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

(繼承來源 MarshalByRefObject)
Parse(TextReader)

將指定文字資料流中所讀取的程式碼編譯成 CodeCompileUnitCompiles the code read from the specified text stream into a CodeCompileUnit.

(繼承來源 CodeDomProvider)
Supports(GeneratorSupport)

傳回值,指出是否提供指定程式碼產生支援。Returns a value indicating whether the specified code generation support is provided.

(繼承來源 CodeDomProvider)
ToString()

傳回任何包含 Component 名稱的 StringReturns a String containing the name of the Component, if any. 不應覆寫此方法。This method should not be overridden.

(繼承來源 Component)

事件

Disposed

當此元件由 Dispose() 方法的呼叫處置時發生。Occurs when the component is disposed by a call to the Dispose() method.

(繼承來源 Component)

安全性

LinkDemand
針對立即呼叫者的完全信任的。for full trust for the immediate caller. 此成員無法由部分信任程式碼使用。This class cannot be used by partially trusted code.

InheritanceDemand
適用於繼承者的完全信任的。for full trust for inheritors. 此類別無法由部分信任程式碼繼承。This class cannot be inherited by partially trusted code.

適用於

另請參閱