Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

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)
    
    ' Build the source file name with the appropriate
    ' language extension.
    Dim sourceFile As String
    If provider.FileExtension.StartsWith(".")
        sourceFile = "TestGraph" + provider.FileExtension
    Else
        sourceFile = "TestGraph." + provider.FileExtension
    End If

    ' Obtain an ICodeGenerator from the CodeDomProvider.
    Dim gen As ICodeGenerator = provider.CreateGenerator()

    ' Create an IndentedTextWriter, constructed with
    ' a StreamWriter to the source file.
    Dim tw As New IndentedTextWriter(New StreamWriter(sourceFile, 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)
{
    // Build the source file name with the appropriate
    // language extension.
    String sourceFile;
    if (provider.FileExtension[0] == '.')
    {
        sourceFile = "TestGraph" + provider.FileExtension;
    }
    else 
    {
        sourceFile = "TestGraph." + provider.FileExtension;
    }

    // Obtain an ICodeGenerator from the CodeDomProvider.
    ICodeGenerator gen = provider.CreateGenerator();
    // Create an IndentedTextWriter, constructed with
    // a StreamWriter to the source file.
    IndentedTextWriter tw = new IndentedTextWriter(new StreamWriter(sourceFile, false), "    ");
    // Generate source code using the code generator.
    gen.GenerateCodeFromCompileUnit(compileunit, tw, new CodeGeneratorOptions());
    // Close the output file.
    tw.Close();            
}

[C++] 
static void GenerateCode(CodeDomProvider* provider, CodeCompileUnit* compileunit)
{
    // Build the source file name with the appropriate
    // language extension.
    String *sourceFile;
    if (provider->FileExtension->StartsWith(S"."))
    {
        sourceFile = String::Concat(S"TestGraph", 
            provider->FileExtension);
    }
    else 
    {
        sourceFile = String::Concat(S"TestGraph.", 
            provider->FileExtension);
    }   

    // Obtain an ICodeGenerator* from the CodeDomProvider.
    ICodeGenerator* gen = provider->CreateGenerator();
    // Create an IndentedTextWriter, constructed with
    // a StreamWriter to the source file.
    IndentedTextWriter* tw = new IndentedTextWriter(new StreamWriter(sourceFile, 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

Show:
© 2015 Microsoft