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");

System.Console.ReadLine();

}

}

}

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

CodeMethodInvokeExpression(readLineRef);

// Add Main() method to a class

CodeTypeDeclaration theClass = new CodeTypeDeclaration();

theClass.Members.Add(objMainMethod);

theClass.Name = "EntryPoint";

// Add both the code of WriteLine and Readline

objMainMethod.Statements.Add(writeLine);

objMainMethod.Statements.Add(readLine);

// Add namespace and add class

CodeNamespace ns = new CodeNamespace("InterviewQuestions");

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

ns.Types.Add(theClass);

// Generate the Compile Unit

CodeCompileUnit unit = new CodeCompileUnit();

unit.Namespaces.Add(ns);

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
What is Extensible Markup Language (XML). XML is an easy and flexible mark-up language in the text format. Nowadays, it is widely used to exchange a large variety of data over

What is Reference counting in COM? The Reference counting is a memory management method used to count the number of times an object has a pointer referring to it. The very firs

What is difference between data set and data reader? Some major differences between dataset and datareader are as folows :- 1) The DataReader gives forward-only and read-onl

What are events? As compared to the delegates events works with source and listener methodology. So the listeners who are interested in receiving few events they subscribe to t

The project aims at converting a rough hand drawn diagram to a neat diagram. Is it a good project for B.Tech level?Please tell how to go about it??

Accepting a List of 5 Items - C# Program Hello, I've been trying so many codes for this but those codes didn't work well. Please write the code for Accepting a List of 5 Items

What are resource files? The Resource files are the files which contain program resources. Many programmers think that resource files are used only for storing strings. But you

Create login page: Consider the login page for a typical webmail service, such as GMail or Outlook Mail. Your task for this assignment is to design a test plan for such a logi

Define Network? A network is a set of devices linked by physical media links. A network is recursively is a connection of two or more nodes by a physical link or two or more

What are the ways of parsing XML document? The XML parser sits in between the XML document and the application who want to use the XML document. The Parser exposes a set of wel