Category Archives: Code Generation

Source code generation example using CodeDom

This example demonstrates how to generate source code automatically using .NET framework. It is also intended to serve as a quick reference of common tasks when coding against CodeDom. The example generates a Ford Focus class type in Automobile Namespace and the class inherits Car class. The generated class source code is at the bottom of this article.

// Create a compile unit
CodeCompileUnit compileUnit = new CodeCompileUnit();
// Define a Namespace
CodeNamespace automobileNamespace = new CodeNamespace("Automobile");
// Import Namespaces
automobileNamespace.Imports.Add(new CodeNamespaceImport("System"));


Importing namespaces in this manner puts the using directive in the Automobile namespace scope. Difference
of putting using inside or outside of the namespace is explained at
Should Usings be inside or outside the namespace.
Read the rest of this entry »

Leave a comment

Posted by on July 30, 2012 in .NET, C#, Code Generation


Tags: ,