VBCodeProvider Class

VBCodeProvider Class

 

Provides access to instances of the Visual Basic code generator and code compiler.

Namespace:   Microsoft.VisualBasic
Assembly:  System (in System.dll)

System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      System.CodeDom.Compiler.CodeDomProvider
        Microsoft.VisualBasic.VBCodeProvider

[PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]
public class VBCodeProvider : CodeDomProvider

NameDescription
System_CAPS_pubmethodVBCodeProvider()

Initializes a new instance of the VBCodeProvider class.

System_CAPS_pubmethodVBCodeProvider(IDictionary<String, String>)

Initializes a new instance of the VBCodeProvider class by using the specified provider options.

NameDescription
System_CAPS_protpropertyCanRaiseEvents

Gets a value indicating whether the component can raise an event.(Inherited from Component.)

System_CAPS_pubpropertyContainer

Gets the IContainer that contains the Component.(Inherited from Component.)

System_CAPS_protpropertyDesignMode

Gets a value that indicates whether the Component is currently in design mode.(Inherited from Component.)

System_CAPS_protpropertyEvents

Gets the list of event handlers that are attached to this Component.(Inherited from Component.)

System_CAPS_pubpropertyFileExtension

Gets the file name extension to use when creating source code files.(Overrides CodeDomProvider.FileExtension.)

System_CAPS_pubpropertyLanguageOptions

Gets a language features identifier.(Overrides CodeDomProvider.LanguageOptions.)

System_CAPS_pubpropertySite

Gets or sets the ISite of the Component.(Inherited from Component.)

NameDescription
System_CAPS_pubmethodCompileAssemblyFromDom(CompilerParameters, CodeCompileUnit[])

Compiles an assembly based on the System.CodeDom trees contained in the specified array of CodeCompileUnit objects, using the specified compiler settings.(Inherited from CodeDomProvider.)

System_CAPS_pubmethodCompileAssemblyFromFile(CompilerParameters, String[])

Compiles an assembly from the source code contained in the specified files, using the specified compiler settings.(Inherited from CodeDomProvider.)

System_CAPS_pubmethodCompileAssemblyFromSource(CompilerParameters, String[])

Compiles an assembly from the specified array of strings containing source code, using the specified compiler settings.(Inherited from CodeDomProvider.)

System_CAPS_pubmethodCreateCompiler()

Obsolete.Gets an instance of the Visual Basic code compiler.(Overrides CodeDomProvider.CreateCompiler().)

System_CAPS_pubmethodCreateEscapedIdentifier(String)

Creates an escaped identifier for the specified value.(Inherited from CodeDomProvider.)

System_CAPS_pubmethodCreateGenerator()

Obsolete.Gets an instance of the Visual Basic code generator.(Overrides CodeDomProvider.CreateGenerator().)

System_CAPS_pubmethodCreateGenerator(String)

When overridden in a derived class, creates a new code generator using the specified file name for output.(Inherited from CodeDomProvider.)

System_CAPS_pubmethodCreateGenerator(TextWriter)

When overridden in a derived class, creates a new code generator using the specified TextWriter for output.(Inherited from CodeDomProvider.)

System_CAPS_pubmethodCreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodCreateParser()

Obsolete.When overridden in a derived class, creates a new code parser.(Inherited from CodeDomProvider.)

System_CAPS_pubmethodCreateValidIdentifier(String)

Creates a valid identifier for the specified value.(Inherited from CodeDomProvider.)

System_CAPS_pubmethodDispose()

Releases all resources used by the Component.(Inherited from Component.)

System_CAPS_protmethodDispose(Boolean)

Releases the unmanaged resources used by the Component and optionally releases the managed resources.(Inherited from Component.)

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection.(Inherited from Component.)

System_CAPS_pubmethodGenerateCodeFromCompileUnit(CodeCompileUnit, TextWriter, CodeGeneratorOptions)

Generates code for the specified Code Document Object Model (CodeDOM) compilation unit and sends it to the specified text writer, using the specified options.(Inherited from CodeDomProvider.)

System_CAPS_pubmethodGenerateCodeFromExpression(CodeExpression, TextWriter, CodeGeneratorOptions)

Generates code for the specified Code Document Object Model (CodeDOM) expression and sends it to the specified text writer, using the specified options.(Inherited from CodeDomProvider.)

System_CAPS_pubmethodGenerateCodeFromMember(CodeTypeMember, TextWriter, CodeGeneratorOptions)

Generates code for the specified class member using the specified text writer and code generator options.(Overrides CodeDomProvider.GenerateCodeFromMember(CodeTypeMember, TextWriter, CodeGeneratorOptions).)

System_CAPS_pubmethodGenerateCodeFromNamespace(CodeNamespace, TextWriter, CodeGeneratorOptions)

Generates code for the specified Code Document Object Model (CodeDOM) namespace and sends it to the specified text writer, using the specified options.(Inherited from CodeDomProvider.)

System_CAPS_pubmethodGenerateCodeFromStatement(CodeStatement, TextWriter, CodeGeneratorOptions)

Generates code for the specified Code Document Object Model (CodeDOM) statement and sends it to the specified text writer, using the specified options.(Inherited from CodeDomProvider.)

System_CAPS_pubmethodGenerateCodeFromType(CodeTypeDeclaration, TextWriter, CodeGeneratorOptions)

Generates code for the specified Code Document Object Model (CodeDOM) type declaration and sends it to the specified text writer, using the specified options.(Inherited from CodeDomProvider.)

System_CAPS_pubmethodGetConverter(Type)

Gets a TypeConverter for the specified type of object.(Overrides CodeDomProvider.GetConverter(Type).)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodGetService(Type)

Returns an object that represents a service provided by the Component or by its Container.(Inherited from Component.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodGetTypeOutput(CodeTypeReference)

Gets the type indicated by the specified CodeTypeReference.(Inherited from CodeDomProvider.)

System_CAPS_pubmethodInitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodIsValidIdentifier(String)

Returns a value that indicates whether the specified value is a valid identifier for the current language.(Inherited from CodeDomProvider.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodParse(TextReader)

Compiles the code read from the specified text stream into a CodeCompileUnit.(Inherited from CodeDomProvider.)

System_CAPS_pubmethodSupports(GeneratorSupport)

Returns a value indicating whether the specified code generation support is provided.(Inherited from CodeDomProvider.)

System_CAPS_pubmethodToString()

Returns a String containing the name of the Component, if any. This method should not be overridden.(Inherited from Component.)

NameDescription
System_CAPS_pubeventDisposed

Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)

This class provides methods that can be used to retrieve instances of the Visual Basic ICodeGenerator and ICodeCompiler implementations.

System_CAPS_noteNote

This class contains a link demand and an inheritance demand at the class level that applies to all members. A SecurityException is thrown when either the immediate caller or the derived class does not have full-trust permission.

The following example uses either the C# or Visual Basic code provider to compile a source file. 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;
}

SecurityAction.LinkDemand

for full trust for the immediate caller. This class cannot be used by partially trusted code.

SecurityAction.InheritanceDemand

for full trust for inheritors. This class cannot be inherited by partially trusted code.

.NET Framework
Available since 1.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft