System.CodeDom.Compiler.ICodeGenerator Interface

Assembly: System.dll
Namespace: System.CodeDom.Compiler
Summary
Provides an interface for generating code.
C# Syntax:
public interface ICodeGenerator
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.

Implementations of this interface are commonly used in conjunction with the ICodeCompiler implementation for the same language, which can compile

See also:
System.CodeDom.Compiler Namespace

System.CodeDom.Compiler.ICodeGenerator Member List:

Public Methods
CreateEscapedIdentifier Creates an escaped identifier for the specified value.
CreateValidIdentifier Creates a valid identifier for the specified value.
GenerateCodeFromCompileUnit Generates code for the specified Code Document Object Model (CodeDOM) compilation unit and outputs it to the specified text writer using the specified options.
GenerateCodeFromExpression Generates code for the specified Code Document Object Model (CodeDOM) expression and outputs it to the specified text writer.
GenerateCodeFromNamespace Generates code for the specified Code Document Object Model (CodeDOM) namespace and outputs it to the specified text writer using the specified options.
GenerateCodeFromStatement Generates code for the specified Code Document Object Model (CodeDOM) statement and outputs it to the specified text writer using the specified options.
GenerateCodeFromType Generates code for the specified Code Document Object Model (CodeDOM) type declaration and outputs it to the specified text writer using the specified options.
GetTypeOutput Gets the type indicated by the specified CodeTypeReference.
IsValidIdentifier Gets a value that indicates whether the specified value is a valid identifier for the current language.
Supports Gets a value indicating whether the generator provides the specified GeneratorSupport.
ValidateIdentifier Tests the specified identifier for validity in the current language and source code and throws an exception if the specified value is not a valid identifier.

System.CodeDom.Compiler.ICodeGenerator Member Details

Method: CreateEscapedIdentifier(
   string value
)
Summary
Creates an escaped identifier for the specified value.
C# Syntax:
string CreateEscapedIdentifier(
   string value
);
Parameters:

value

The string to create an escaped identifier for.

Return Value:
The escaped identifier for the value.
Remarks
ICodeGenerator.CreateEscapedIdentifier tests whether the identifier conflicts with reserved or language keywords, and returns an equivalent name with language-specific escape code formatting. Usually the returned identifier will remain basically the same but will have escape code formatting added to differentiate the identifier from the keyword.
See also:
ICodeGenerator.CreateValidIdentifier

Return to top


Method: CreateValidIdentifier(
   string value
)
Summary
Creates a valid identifier for the specified value.
C# Syntax:
string CreateValidIdentifier(
   string value
);
Parameters:

value

The string to generate a valid identifier for.

Return Value:
A valid identifier for the specified value.
Remarks
ICodeGenerator.CreateValidIdentifier tests whether the identifier conflicts with reserved or language keywords, and returns a valid identifier name that does not conflict. Usually the returned identifier will remain basically the same but will be slightly modified to differentiate the identifier from the keyword.
See also:
ICodeGenerator.CreateEscapedIdentifier

Return to top


Method: GenerateCodeFromCompileUnit(
   CodeCompileUnit e,
   TextWriter w,
   CodeGeneratorOptions o
)
Summary
Generates code for the specified Code Document Object Model (CodeDOM) compilation unit and outputs it to the specified text writer using the specified options.
C# Syntax:
void GenerateCodeFromCompileUnit(
   CodeCompileUnit e,
   TextWriter w,
   CodeGeneratorOptions o
);
Parameters:

e

A CodeCompileUnit to generate code for.

w

The TextWriter to output code to.

o

A CodeGeneratorOptions that indicates the options to use for generating code.

See also:
TextWriter | CodeExpression

Return to top


Method: GenerateCodeFromExpression(
   CodeExpression e,
   TextWriter w,
   CodeGeneratorOptions o
)
Summary
Generates code for the specified Code Document Object Model (CodeDOM) expression and outputs it to the specified text writer.
C# Syntax:
void GenerateCodeFromExpression(
   CodeExpression e,
   TextWriter w,
   CodeGeneratorOptions o
);
Parameters:

e

A CodeExpression that indicates the expression to generate code for.

w

The TextWriter to output code to.

o

A CodeGeneratorOptions that indicates the options to use for generating code.

See also:
TextWriter | CodeExpression

Return to top


Method: GenerateCodeFromNamespace(
   CodeNamespace e,
   TextWriter w,
   CodeGeneratorOptions o
)
Summary
Generates code for the specified Code Document Object Model (CodeDOM) namespace and outputs it to the specified text writer using the specified options.
C# Syntax:
void GenerateCodeFromNamespace(
   CodeNamespace e,
   TextWriter w,
   CodeGeneratorOptions o
);
Parameters:

e

A CodeNamespace that indicates the namespace to generate code for.

w

The TextWriter to output code to.

o

A CodeGeneratorOptions that indicates the options to use for generating code.

See also:
TextWriter | CodeExpression

Return to top


Method: GenerateCodeFromStatement(
   CodeStatement e,
   TextWriter w,
   CodeGeneratorOptions o
)
Summary
Generates code for the specified Code Document Object Model (CodeDOM) statement and outputs it to the specified text writer using the specified options.
C# Syntax:
void GenerateCodeFromStatement(
   CodeStatement e,
   TextWriter w,
   CodeGeneratorOptions o
);
Parameters:

e

A CodeStatement containing the CodeDOM elements to translate.

w

The TextWriter to output code to.

o

A CodeGeneratorOptions that indicates the options to use for generating code.

See also:
TextWriter | CodeExpression

Return to top


Method: GenerateCodeFromType(
   CodeTypeDeclaration e,
   TextWriter w,
   CodeGeneratorOptions o
)
Summary
Generates code for the specified Code Document Object Model (CodeDOM) type declaration and outputs it to the specified text writer using the specified options.
C# Syntax:
void GenerateCodeFromType(
   CodeTypeDeclaration e,
   TextWriter w,
   CodeGeneratorOptions o
);
Parameters:

e

A CodeTypeDeclaration that indicates the type to generate code for.

w

The TextWriter to output code to.

o

A CodeGeneratorOptions that indicates the options to use for generating code.

See also:
TextWriter | CodeExpression

Return to top


Method: GetTypeOutput(
   CodeTypeReference type
)
Summary
Gets the type indicated by the specified CodeTypeReference.
C# Syntax:
string GetTypeOutput(
   CodeTypeReference type
);
Parameters:

type

A CodeTypeReference that indicates the type to return.

Return Value:
A text representation of the specified type for the language this code generator is designed to generate code in. For example, in Visual Basic, passing in type System.Int32 will return "Integer".

Return to top


Method: IsValidIdentifier(
   string value
)
Summary
Gets a value that indicates whether the specified value is a valid identifier for the current language.
C# Syntax:
bool IsValidIdentifier(
   string value
);
Parameters:

value

The value to test for being a valid identifier.

Return Value:
true if the value parameter is a valid identifier; otherwise, false.
Remarks
Identifiers in computer languages must fit certain rules. This method provides for testing of whether an identifier conforms to the rules for each language. When implementing ICodeGenerator.IsValidIdentifier in a derived class, design the method to test the value passed to it, and return true only if the value fits the rules of the language and does not conflict with a keyword.

Return to top


Method: Supports(
   GeneratorSupport supports
)
Summary
Gets a value indicating whether the generator provides the specified GeneratorSupport.
C# Syntax:
bool Supports(
   GeneratorSupport supports
);
Parameters:

supports

The capabilities to test the generator for.

Return Value:
true if the specified capabilities are supported; otherwise, false.
Remarks
This method can be called with a number of GeneratorSupport flags at once to test for a set of capabilities by joining a set of appropriate capability flags together with a binary "Or" operator (|).

Return to top


Method: ValidateIdentifier(
   string value
)
Summary
Tests the specified identifier for validity in the current language and source code and throws an exception if the specified value is not a valid identifier.
C# Syntax:
void ValidateIdentifier(
   string value
);
Parameters:

value

The value to validate.

Remarks
Rather than returning a value, this method throws an exception if the specified identifier is not valid for the current language and source code. Typically, the exception is an ArgumentException. This method enables a code generator that implements this method to produce an informative error message based upon the value of an invalid identifier.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.