How to: Create a Class Using CodeDOM

 

The following procedures illustrate how to create and compile a CodeDOM graph that generates a class containing two fields, three properties, a method, a constructor, and an entry point.

  1. Create a console application that will use CodeDOM code to generate the source code for a class.

    In this example, the generating class is named Sample, and the generated code is a class named CodeDOMCreatedClass in a file named SampleCode.

  2. In the generating class, initialize the CodeDOM graph and use CodeDOM methods to define the members, constructor, and entry point (Main method) of the generated class.

    In this example, the generated class has two fields, three properties, a constructor, a method, and a Main method.

  3. In the generating class, create a language-specific code provider and call its GenerateCodeFromCompileUnit method to generate the code from the graph.

  4. Compile and execute the application to generate the code.

    In this example, the generated code is in a file named SampleCode. Compile and execute that code to see the sample output.

To create the application that will execute the CodeDOM code

  • Create a console application class to contain the CodeDOM code. Define the global fields that are to be used in the class to reference the assembly (CodeCompileUnit) and class (CodeTypeDeclaration), specify the name of the generated source file, and declare the Main method.

    [!CODE [CodeDOM Class Sample Main#1](../CodeSnippet/VS_Snippets_CLR/CodeDOM Class Sample Main#1)]

To initialize the CodeDOM graph

  • In the constructor for the console application class, initialize the assembly and class, and add the appropriate declarations to the CodeDOM graph.

    [!CODE [CodeDOM Class Sample#2](../CodeSnippet/VS_Snippets_CLR/CodeDOM Class Sample#2)]

To add members to the CodeDOM graph

  • Add fields to the CodeDOM graph by adding CodeMemberField objects to the Members property of the class.

    [!CODE [CodeDOM Class Sample#3](../CodeSnippet/VS_Snippets_CLR/CodeDOM Class Sample#3)]

  • Add properties to the CodeDOM graph by adding CodeMemberProperty objects to the Members property of the class.

    [!CODE [CodeDOM Class Sample#4](../CodeSnippet/VS_Snippets_CLR/CodeDOM Class Sample#4)]

  • Add a method to the CodeDOM graph by adding a CodeMemberMethod object to the Members property of the class.

    [!CODE [CodeDOM Class Sample#5](../CodeSnippet/VS_Snippets_CLR/CodeDOM Class Sample#5)]

  • Add a constructor to the CodeDOM graph by adding a CodeConstructor object to the Members property of the class.

    [!CODE [CodeDOM Class Sample#6](../CodeSnippet/VS_Snippets_CLR/CodeDOM Class Sample#6)]

  • Add an entry point to the CodeDOM graph by adding a CodeEntryPointMethod object to the Members property of the class.

    [!CODE [CodeDOM Class Sample#7](../CodeSnippet/VS_Snippets_CLR/CodeDOM Class Sample#7)]

To generate the code from the CodeDOM graph

  • Generate source code from the CodeDOM graph by calling the GenerateCodeFromCompileUnit method.

    [!CODE [CodeDOM Class Sample#8](../CodeSnippet/VS_Snippets_CLR/CodeDOM Class Sample#8)]

To create the graph and generate the code

  1. Add the methods created in the preceding steps to the Main method defined in the first step.

    [!CODE [CodeDOM Class Sample#9](../CodeSnippet/VS_Snippets_CLR/CodeDOM Class Sample#9)]

  2. Compile and execute the generating class.

The following code example shows the code from the preceding steps.

[!CODE [CodeDOM Class Sample#1](../CodeSnippet/VS_Snippets_CLR/CodeDOM Class Sample#1)]

When the preceding example is compiled and executed, it produces the following source code.

[!CODE [CodeDOM Class Sample#99](../CodeSnippet/VS_Snippets_CLR/CodeDOM Class Sample#99)]

The generated source code produces the following output when compiled and executed.

The object:  
 width = 5.3,  
 height = 6.9,  
 area = 36.57  

  • This code example requires the FullTrust permission set to execute successfully.

Using the CodeDOM
Generating and Compiling Source Code from a CodeDOM Graph

Show: