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
I need Export Data using JSON Project Description: We could like to share our data with other websites via JSON. We are presently using Entity Framework with MySQL. We req

How many types of diagrams are there in UML? There are nine types of diagrams in the UML as shown below:- 1)Use case diagram: The use case diagram describe "WHAT" of a

What is XML? Extensible markup language (XML) is all about describing the data. Below is a XML which describes an invoice data. Shoes   12 10

C# API to grab option price from CBOE web site Project Description: On the web page below, user will download option prices of Stock or Index. For example, if you put "VXX" i

Project Description: I have a site that's previously fully developed, and need to fix some small problems (Text editor posting error, and a Server error caused by some Nhibernat

Require Guys in Gaming Tracks Company for a mobile game developing Project Description: I want a team of game developers for life-time as mobile dev team in my company. So, i

I need .NET Application Decompile Project Description: We have a minute .net utility which we want to decompile back into .net project. .Net application can be made availa

Introduction This assignment aims to give you a "real world experience" that occurs far too often in the workplace. You have been hired to complete as much as possible a project

The Problem with finalize is that the garbage collection has to make 2  rounds in order to remove the  objects which have finalize methods. The figure below will make things cle

What are the ways to implement locking in ADO.NET? There are many ways to implement locking using ADO.NET which are shown below:- 1)Whenever we call "Update" method of DataA