CodeDomProvider Class
Provides a base class for CodeDomProvider implementations. This class is abstract.
Assembly: System (in System.dll)
System::MarshalByRefObject
System.ComponentModel::Component
System.CodeDom.Compiler::CodeDomProvider
Microsoft.CSharp::CSharpCodeProvider
Microsoft.JScript::JScriptCodeProvider
Microsoft.VisualBasic::VBCodeProvider
| Name | Description | |
|---|---|---|
![]() | CodeDomProvider() | Initializes a new instance of the CodeDomProvider class. |
| Name | Description | |
|---|---|---|
![]() | CanRaiseEvents | Gets a value indicating whether the component can raise an event.(Inherited from Component.) |
![]() | Container | Gets the IContainer that contains the Component.(Inherited from Component.) |
![]() | DesignMode | |
![]() | Events | |
![]() | FileExtension | Gets the default file name extension to use for source code files in the current language. |
![]() | LanguageOptions | Gets a language features identifier. |
![]() | Site |
| Name | Description | |
|---|---|---|
![]() | CompileAssemblyFromDom(CompilerParameters^, array<CodeCompileUnit^>^) | Compiles an assembly based on the System.CodeDom trees contained in the specified array of CodeCompileUnit objects, using the specified compiler settings. |
![]() | CompileAssemblyFromFile(CompilerParameters^, array<String^>^) | Compiles an assembly from the source code contained in the specified files, using the specified compiler settings. |
![]() | CompileAssemblyFromSource(CompilerParameters^, array<String^>^) | Compiles an assembly from the specified array of strings containing source code, using the specified compiler settings. |
![]() | CreateCompiler() | Obsolete. When overridden in a derived class, creates a new code compiler. |
![]() | CreateEscapedIdentifier(String^) | Creates an escaped identifier for the specified value. |
![]() | CreateGenerator() | Obsolete. When overridden in a derived class, creates a new code generator. |
![]() | CreateGenerator(String^) | When overridden in a derived class, creates a new code generator using the specified file name for output. |
![]() | CreateGenerator(TextWriter^) | When overridden in a derived class, creates a new code generator using the specified TextWriter for output. |
![]() | CreateObjRef(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.) |
![]() | CreateParser() | Obsolete. When overridden in a derived class, creates a new code parser. |
![]() ![]() | CreateProvider(String^) | Gets a CodeDomProvider instance for the specified language. |
![]() ![]() | CreateProvider(String^, IDictionary<String^, String^>^) | Gets a CodeDomProvider instance for the specified language and provider options. |
![]() | CreateValidIdentifier(String^) | Creates a valid identifier for the specified value. |
![]() | Dispose() | |
![]() | Dispose(Boolean) | |
![]() | Equals(Object^) | Determines whether the specified object is equal to the current object.(Inherited from Object.) |
![]() | Finalize() | |
![]() | GenerateCodeFromCompileUnit(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. |
![]() | GenerateCodeFromExpression(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. |
![]() | GenerateCodeFromMember(CodeTypeMember^, TextWriter^, CodeGeneratorOptions^) | Generates code for the specified Code Document Object Model (CodeDOM) member declaration and sends it to the specified text writer, using the specified options. |
![]() | GenerateCodeFromNamespace(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. |
![]() | GenerateCodeFromStatement(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. |
![]() | GenerateCodeFromType(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. |
![]() ![]() | GetAllCompilerInfo() | Returns the language provider and compiler configuration settings for this computer. |
![]() ![]() | GetCompilerInfo(String^) | Returns the language provider and compiler configuration settings for the specified language. |
![]() | GetConverter(Type^) | Gets a TypeConverter for the specified data type. |
![]() | GetHashCode() | Serves as the default hash function. (Inherited from Object.) |
![]() ![]() | GetLanguageFromExtension(String^) | Returns a language name associated with the specified file name extension, as configured in the CodeDomProvider compiler configuration section. |
![]() | GetLifetimeService() | Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.) |
![]() | GetService(Type^) | |
![]() | GetType() | |
![]() | GetTypeOutput(CodeTypeReference^) | Gets the type indicated by the specified CodeTypeReference. |
![]() | InitializeLifetimeService() | Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.) |
![]() ![]() | IsDefinedExtension(String^) | Tests whether a file name extension has an associated CodeDomProvider implementation configured on the computer. |
![]() ![]() | IsDefinedLanguage(String^) | Tests whether a language has a CodeDomProvider implementation configured on the computer. |
![]() | IsValidIdentifier(String^) | Returns a value that indicates whether the specified value is a valid identifier for the current language. |
![]() | MemberwiseClone() | |
![]() | MemberwiseClone(Boolean) | Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.) |
![]() | Parse(TextReader^) | Compiles the code read from the specified text stream into a CodeCompileUnit. |
![]() | Supports(GeneratorSupport) | Returns a value indicating whether the specified code generation support is provided. |
![]() | ToString() |
A CodeDomProvider can be used to create and retrieve instances of code generators and code compilers. Code generators can be used to generate code in a particular language, and code compilers can be used to compile code into assemblies.
Note |
|---|
In the .NET Framework 2.0, the methods made available in the code generator and code compiler are available directly from the code provider. You do not need to call CreateGenerator or CreateCompiler to access the methods, and those methods are marked as obsolete. This applies to preexisting as well as new code provider implementations. |
A CodeDomProvider implementation typically provides code generation and/or code compilation interfaces for generating code and managing compilation for a single programming language. Several languages are supported by CodeDomProvider implementations that ship with the Windows Software Development Kit (SDK). These languages include C#, Visual Basic, C++, and JScript. Developers or compiler vendors can implement the ICodeGenerator and ICodeCompiler interfaces and provide a CodeDomProvider that extends CodeDOM support to other programming languages.
The <system.codedom> Element in the machine configuration file (Machine.config) provides a mechanism for developers and compiler vendors to add configuration settings for additional CodeDomProvider implementations.
The CodeDomProvider class provides static methods to discover and enumerate the CodeDomProvider implementations on a computer. The GetAllCompilerInfo method returns the settings for all CodeDomProvider implementations on a computer. The GetCompilerInfo method returns the settings for a specific CodeDomProvider implementation, based on the programming language name. The CreateProvider method returns an instance of a CodeDomProvider implementation for a specific language.
For more details on language provider settings in the configuration file, see Compiler and Language Provider Settings Schema.
Note |
|---|
This class makes a link demand and an inheritance demand at the class level. A SecurityException is thrown if either the immediate caller or the derived class does not have full trust permission. For details about security demands, see Link Demands and Inheritance Demands. |
Notes to Inheritors:
In the .NET Framework versions 1.0 and 1.1, code providers consist of implementations of CodeDomProvider, ICodeGenerator, ICodeParser, and ICodeCompiler. In the .NET Framework 2.0, the CreateGenerator, CreateParser, and CreateCompiler methods are obsolete, and the methods of ICodeGenerator and ICodeCompiler are directly available in the CodeDomProvider class. You should override those methods in your code provider implementation and not call the base methods.
The following example program can generate and compile source code based on a CodeDOM model of a program that prints "Hello World" using the Console class. A Windows Forms user interface is provided. The user can select the target programming language from several selections: C#, Visual Basic, and JScript.
#using <System.Windows.Forms.dll> #using <System.Drawing.dll> #using <System.dll> #using <Microsoft.JScript.dll> using namespace System; using namespace System::CodeDom; using namespace System::CodeDom::Compiler; using namespace System::Collections; using namespace System::ComponentModel; using namespace System::Diagnostics; using namespace System::Drawing; using namespace System::IO; using namespace System::Windows::Forms; using namespace Microsoft::CSharp; using namespace Microsoft::VisualBasic; using namespace Microsoft::JScript; using namespace System::Security::Permissions; // This example demonstrates building a Hello World program graph // using System.CodeDom elements. It calls code generator and // code compiler methods to build the program using CSharp, VB, or // JScript. A Windows Forms interface is included. Note: Code // must be compiled and linked with the Microsoft.JScript assembly. namespace CodeDOMExample { [PermissionSet(SecurityAction::Demand, Name="FullTrust")] public ref class CodeDomExample { public: // Build a Hello World program graph using // System::CodeDom types. static CodeCompileUnit^ BuildHelloWorldGraph() { // Create a new CodeCompileUnit to contain // the program graph. CodeCompileUnit^ compileUnit = gcnew CodeCompileUnit; // Declare a new namespace called Samples. CodeNamespace^ samples = gcnew CodeNamespace( "Samples" ); // Add the new namespace to the compile unit. compileUnit->Namespaces->Add( samples ); // Add the new namespace import for the System namespace. samples->Imports->Add( gcnew CodeNamespaceImport( "System" ) ); // Declare a new type called Class1. CodeTypeDeclaration^ class1 = gcnew CodeTypeDeclaration( "Class1" ); // Add the new type to the namespace's type collection. samples->Types->Add( class1 ); // Declare a new code entry point method. CodeEntryPointMethod^ start = gcnew CodeEntryPointMethod; // Create a type reference for the System::Console class. CodeTypeReferenceExpression^ csSystemConsoleType = gcnew CodeTypeReferenceExpression( "System.Console" ); // Build a Console::WriteLine statement. CodeMethodInvokeExpression^ cs1 = gcnew CodeMethodInvokeExpression( csSystemConsoleType,"WriteLine", gcnew CodePrimitiveExpression("Hello World!") ); // Add the WriteLine call to the statement collection. start->Statements->Add( cs1 ); // Build another Console::WriteLine statement. CodeMethodInvokeExpression^ cs2 = gcnew CodeMethodInvokeExpression( csSystemConsoleType,"WriteLine", gcnew CodePrimitiveExpression( "Press the Enter key to continue." ) ); // Add the WriteLine call to the statement collection. start->Statements->Add( cs2 ); // Build a call to System::Console::ReadLine. CodeMethodReferenceExpression^ csReadLine = gcnew CodeMethodReferenceExpression( csSystemConsoleType, "ReadLine" ); CodeMethodInvokeExpression^ cs3 = gcnew CodeMethodInvokeExpression( csReadLine, gcnew array<CodeExpression^>(0) ); // Add the ReadLine statement. start->Statements->Add( cs3 ); // Add the code entry point method to // the Members collection of the type. class1->Members->Add( start ); return compileUnit; } static void GenerateCode( CodeDomProvider^ provider, CodeCompileUnit^ compileunit ) { // Build the source file name with the appropriate // language extension. String^ sourceFile; if ( provider->FileExtension->StartsWith( "." ) ) { sourceFile = String::Concat( "TestGraph", provider->FileExtension ); } else { sourceFile = String::Concat( "TestGraph.", provider->FileExtension ); } // Create an IndentedTextWriter, constructed with // a StreamWriter to the source file. IndentedTextWriter^ tw = gcnew IndentedTextWriter( gcnew StreamWriter( sourceFile,false )," " ); // Generate source code using the code generator. provider->GenerateCodeFromCompileUnit( compileunit, tw, gcnew CodeGeneratorOptions ); // Close the output file. tw->Close(); } static CompilerResults^ CompileCode( CodeDomProvider^ provider, String^ sourceFile, String^ exeFile ) { // Configure a CompilerParameters that links System.dll // and produces the specified executable file. array<String^>^referenceAssemblies = {"System.dll"}; CompilerParameters^ cp = gcnew CompilerParameters( referenceAssemblies,exeFile,false ); // Generate an executable rather than a DLL file. cp->GenerateExecutable = true; // Invoke compilation. CompilerResults^ cr = provider->CompileAssemblyFromFile( cp, sourceFile ); // Return the results of compilation. return cr; } }; public ref class CodeDomExampleForm: public System::Windows::Forms::Form { private: static System::Windows::Forms::Button^ run_button = gcnew System::Windows::Forms::Button; static System::Windows::Forms::Button^ compile_button = gcnew System::Windows::Forms::Button; static System::Windows::Forms::Button^ generate_button = gcnew System::Windows::Forms::Button; static System::Windows::Forms::TextBox^ textBox1 = gcnew System::Windows::Forms::TextBox; static System::Windows::Forms::ComboBox^ comboBox1 = gcnew System::Windows::Forms::ComboBox; static System::Windows::Forms::Label^ label1 = gcnew System::Windows::Forms::Label; void generate_button_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ ) { CodeDomProvider^ provider = GetCurrentProvider(); CodeDomExample::GenerateCode( provider, CodeDomExample::BuildHelloWorldGraph() ); // Build the source file name with the appropriate // language extension. String^ sourceFile; if ( provider->FileExtension->StartsWith( "." ) ) { sourceFile = String::Concat( "TestGraph", provider->FileExtension ); } else { sourceFile = String::Concat( "TestGraph.", provider->FileExtension ); } // Read in the generated source file and // display the source text. StreamReader^ sr = gcnew StreamReader( sourceFile ); textBox1->Text = sr->ReadToEnd(); sr->Close(); } CodeDomProvider^ GetCurrentProvider() { CodeDomProvider^ provider; if ( String::Compare( dynamic_cast<String^>(this->comboBox1->SelectedItem), "CSharp" ) == 0 ) provider = CodeDomProvider::CreateProvider("CSharp"); else if ( String::Compare( dynamic_cast<String^>(this->comboBox1->SelectedItem), "Visual Basic" ) == 0 ) provider = CodeDomProvider::CreateProvider("VisualBasic"); else if ( String::Compare( dynamic_cast<String^>(this->comboBox1->SelectedItem), "JScript" ) == 0 ) provider = CodeDomProvider::CreateProvider("JScript"); else provider = CodeDomProvider::CreateProvider("CSharp"); return provider; } void compile_button_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ ) { CodeDomProvider^ provider = GetCurrentProvider(); // Build the source file name with the appropriate // language extension. String^ sourceFile = String::Concat( "TestGraph.", provider->FileExtension ); // Compile the source file into an executable output file. CompilerResults^ cr = CodeDomExample::CompileCode( provider, sourceFile, "TestGraph.exe" ); if ( cr->Errors->Count > 0 ) { // Display compilation errors. textBox1->Text = String::Concat( "Errors encountered while building ", sourceFile, " into ", cr->PathToAssembly, ": \r\n\n" ); System::CodeDom::Compiler::CompilerError^ ce; for ( int i = 0; i < cr->Errors->Count; i++ ) { ce = cr->Errors[i]; textBox1->AppendText( String::Concat( ce->ToString(), "\r\n" ) ); } run_button->Enabled = false; } else { textBox1->Text = String::Concat( "Source ", sourceFile, " built into ", cr->PathToAssembly, " with no errors." ); run_button->Enabled = true; } } void run_button_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ ) { Process::Start( "TestGraph.exe" ); } public: CodeDomExampleForm() { this->SuspendLayout(); // Set properties for label1. this->label1->Location = System::Drawing::Point( 395, 20 ); this->label1->Size = System::Drawing::Size( 180, 22 ); this->label1->Text = "Select a programming language:"; // Set properties for comboBox1. this->comboBox1->Location = System::Drawing::Point( 560, 16 ); this->comboBox1->Size = System::Drawing::Size( 190, 23 ); this->comboBox1->Name = "comboBox1"; array<String^>^temp1 = {"CSharp","Visual Basic","JScript"}; this->comboBox1->Items->AddRange( temp1 ); this->comboBox1->Anchor = (System::Windows::Forms::AnchorStyles)(System::Windows::Forms::AnchorStyles::Left | System::Windows::Forms::AnchorStyles::Right | System::Windows::Forms::AnchorStyles::Top); this->comboBox1->SelectedIndex = 0; // Set properties for generate_button. this->generate_button->Location = System::Drawing::Point( 8, 16 ); this->generate_button->Name = "generate_button"; this->generate_button->Size = System::Drawing::Size( 120, 23 ); this->generate_button->Text = "Generate Code"; this->generate_button->Click += gcnew System::EventHandler( this, &CodeDomExampleForm::generate_button_Click ); // Set properties for compile_button. this->compile_button->Location = System::Drawing::Point( 136, 16 ); this->compile_button->Name = "compile_button"; this->compile_button->Size = System::Drawing::Size( 120, 23 ); this->compile_button->Text = "Compile"; this->compile_button->Click += gcnew System::EventHandler( this, &CodeDomExampleForm::compile_button_Click ); // Set properties for run_button. this->run_button->Enabled = false; this->run_button->Location = System::Drawing::Point( 264, 16 ); this->run_button->Name = "run_button"; this->run_button->Size = System::Drawing::Size( 120, 23 ); this->run_button->Text = "Run"; this->run_button->Click += gcnew System::EventHandler( this, &CodeDomExampleForm::run_button_Click ); // Set properties for textBox1. this->textBox1->Anchor = (System::Windows::Forms::AnchorStyles)(System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left | System::Windows::Forms::AnchorStyles::Right); this->textBox1->Location = System::Drawing::Point( 8, 48 ); this->textBox1->Multiline = true; this->textBox1->ScrollBars = System::Windows::Forms::ScrollBars::Vertical; this->textBox1->Name = "textBox1"; this->textBox1->Size = System::Drawing::Size( 744, 280 ); this->textBox1->Text = ""; // Set properties for the CodeDomExampleForm. this->AutoScaleBaseSize = System::Drawing::Size( 5, 13 ); this->ClientSize = System::Drawing::Size( 768, 340 ); this->MinimumSize = System::Drawing::Size( 750, 340 ); array<System::Windows::Forms::Control^>^myControl = {this->textBox1,this->run_button,this->compile_button,this->generate_button,this->comboBox1,this->label1}; this->Controls->AddRange( myControl ); this->Name = "CodeDomExampleForm"; this->Text = "CodeDom Hello World Example"; this->ResumeLayout( false ); } public: ~CodeDomExampleForm() { } }; } [STAThread] int main() { Application::Run( gcnew CodeDOMExample::CodeDomExampleForm ); }
for full trust for the immediate caller. This class cannot be used by partially trusted code.
for full trust for inheritors. This class cannot be inherited by partially trusted code.
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.






