Codedom, DOT NET Programming

What is CodeDom

The "CodeDom" is an object model that represents actually a source code. It is designed for language independent - once you create a "CodeDom" hierarchy for a program we can then generate the source code in any .NET language. So let's try to do some real practical and simple to just get a feel of how powerful "CodeDom" is.

We will try to generate the following code shown below. The below code which will be generated does not do any special thing by just displays a hello message and waits for the key to be clicked.

namespace InterviewQuestions


using System;

public class EntryPoint


public static void Main()


System.Console.WriteLine("Hello from Interview Question series");





The "Codedom" folder at the CD has one "GenerateCode" method that returns "CodeCompileUnit" object. The "CodeDom" is nothing but a full DOM model where every object in the structure shows a code unit. I have put some comments on the code so that the code is self understandable. I have put some comment at the code below so that readers can follow what is exactly happening. When you click the button it generates the "MyCode.cs" and also compiles the "Mycode.exe" in the "bin" folder.

private CodeCompileUnit GenerateCode()


// Definition of the Main method which will be entry point

CodeEntryPointMethod objMainMethod = new CodeEntryPointMethod();

objMainMethod.Name = "Main";

// generate this expression: Console

CodeTypeReferenceExpression consoleType = new CodeTypeReferenceExpression();

consoleType.Type = new CodeTypeReference(typeof(Console));

// Set up the argument list to pass to Console.WriteLine()

CodeExpression[] writeLineArgs = new CodeExpression[1];

CodePrimitiveExpression arg0 = new CodePrimitiveExpression("Hello from Interview

Question series");

writeLineArgs[0] = arg0;

// generate this statement: Console.WriteLine(message)

CodeMethodReferenceExpression writeLineRef = new

CodeMethodReferenceExpression(consoleType, "WriteLine");

CodeMethodInvokeExpression writeLine = new

CodeMethodInvokeExpression(writeLineRef, writeLineArgs);

// generate this statement: Console.ReadLine()

CodeMethodReferenceExpression readLineRef = new

CodeMethodReferenceExpression(consoleType, "ReadLine");

CodeMethodInvokeExpression readLine = new


// Add Main() method to a class

CodeTypeDeclaration theClass = new CodeTypeDeclaration();


theClass.Name = "EntryPoint";

// Add both the code of WriteLine and Readline



// Add namespace and add class

CodeNamespace ns = new CodeNamespace("InterviewQuestions");

ns.Imports.Add(new CodeNamespaceImport("System"));


// Generate the Compile Unit

CodeCompileUnit unit = new CodeCompileUnit();


Posted Date: 9/21/2012 7:18:27 AM | Location : United States

Related Discussions:- Codedom, Assignment Help, Ask Question on Codedom, Get Answer, Expert's Help, Codedom Discussions

Write discussion on Codedom
Your posts are moderated
Related Questions
Game development tutorials required Project Description: I need video tutorials like lynda for game development using unity. You must have developed games before and need to

Question: (a) One problem encountered when designing web applications is that HTTP is a "stateless" protocol. Explain two general approaches to solve the statelessness problem

What are the types of dependencies in cache? Whenever you add an item to the cache, the dependency relationships is define that can force that item to be removed from the cache

Web application as well as site design & development (.NET) We are interested in retentive the services of a solution provider that will design and implement the presentation la

Project Description: I have a basic C# control interface that I want to be accessed remotely. Possible implementation using wrappers. Skills required are C# Programming, Webs

im retrieving the path of image from the database i want to get the image in that path to be displayed in my datagridviewvolumn

Shipping Emails Bulk System Project Description: We need to develop a system for sending emails to a large mass, without using third-party services, administration by us, or

Locate BUG website Project Description: Can you find a bug in easports fifa 14 ultimate team web app (easports/fifa/football-club/ultimate-team) so that i will earn a lot of

Computer programming Project Description: Solving some issues about traffic jam Skills required: C# Programming, Java, C++ Programming, PHP, Software Architecture

Can a .NET web application consume Java web service? Yes Offcourse.Actually Webservices are independent  to language. It depends on WSDL and SOAP. so any one can develope the W