.NET Framework Class Library  

ICodeGenerator Interface

Defines an interface for generating code.

For a list of all members of this type, see ICodeGenerator Members.

[Visual Basic]
Public Interface ICodeGenerator
[C#]
public interface ICodeGenerator
[C++]
public __gc __interface ICodeGenerator
[JScript]
public interface ICodeGenerator

Classes that Implement ICodeGenerator

Class Description
CodeGenerator Provides an example implementation of the ICodeGenerator interface. This class is abstract.

Remarks

Developers of compilers can implement this interface to allow people to dynamically generate code in a particular language. This can be used for a variety of purposes, such as creating code-generating wizards, creating dynamic assemblies with content that can be debugged, and for templated documents with embedded code, such as ASP.NET.

An ICodeGenerator implementation is typically obtained through a call to the CreateGenerator method of CodeDomProvider.

Example

[Visual Basic, C#, C++] The following example uses an ICodeGenerator implementation to generate source code based on the structure of a CodeDOM source code model. The source code model is contained within a CodeCompileUnit that must be supplied to this method. A CodeDomProvider for the target language must also be supplied to this method. For a larger code example that provides a source code model and a user interface, see the code example for the CodeDomProvider class.

[Visual Basic] 
Public Shared Sub GenerateCode(ByVal provider As CodeDomProvider, ByVal compileunit As CodeCompileUnit)
    ' Obtain an ICodeGenerator from a CodeDomProvider class.
    Dim gen As ICodeGenerator = provider.CreateGenerator()
    ' Create a TextWriter to a StreamWriter to an output file.
    Dim tw As New IndentedTextWriter(New StreamWriter("TestGraph.cs", False), "    ")
    ' Generate source code using the code generator.
    gen.GenerateCodeFromCompileUnit(compileunit, tw, New CodeGeneratorOptions())
    ' Close the output file.
    tw.Close()
End Sub 

[C#] 
public static void GenerateCode(CodeDomProvider provider, CodeCompileUnit compileunit)
{
    // Obtain an ICodeGenerator from a CodeDomProvider class.
    ICodeGenerator gen = provider.CreateGenerator();
    // Create a TextWriter to a StreamWriter to an output file.
    IndentedTextWriter tw = new IndentedTextWriter(new StreamWriter("TestGraph.cs", false), "    ");
    // Generate source code using the code generator.
    gen.GenerateCodeFromCompileUnit(compileunit, tw, new CodeGeneratorOptions());
    // Close the output file.
    tw.Close();            
}

[C++] 
public:
   static void GenerateCode(CodeDomProvider* provider, CodeCompileUnit* compileunit) {
      // Obtain an ICodeGenerator* from a CodeDomProvider class.
      ICodeGenerator* gen = provider->CreateGenerator();
      // Create a TextWriter to a StreamWriter to an output file.
      IndentedTextWriter* tw = new IndentedTextWriter(new StreamWriter(S"TestGraph.cs", false), S"    ");
      // Generate source code using the code generator.
      gen->GenerateCodeFromCompileUnit(compileunit, tw, new CodeGeneratorOptions());
      // Close the output file.
      tw->Close();
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.CodeDom.Compiler

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

ICodeGenerator Members | System.CodeDom.Compiler Namespace