Codedom, DOT NET Programming

Assignment Help:

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


Related Discussions:- Codedom

Sorting 2 arrays & merging into 1 - c# program, Sorting 2 Arrays & Merging ...

Sorting 2 Arrays & Merging into 1 - C# Program Hello, i didn't find the correct way to implement Array in my project. I am beginner for this language.  Please suggest me some

V model in testing, What is V model in testing? The V model map's the t...

What is V model in testing? The V model map's the type of test to the stage of development in the project. Figure: - V Model 1) Unit Testing: The Starting fro

What are the differences between asp and asp .net, What are the differences...

What are the differences between ASP and ASP .Net ?  ASP: Code is Interpreted ASP.NET: Code is Compiled   ASP: Business Logic and Presentation Logic are in a one

I need asp.net developer, I am seeking asp.net developers, and will raise d...

I am seeking asp.net developers, and will raise depends on skills and work, we are a web development firm in mexico and i want full time developers that can focus 100 percent my pr

Prepare .net twain application, Prepare .NET TWAIN application to control d...

Prepare .NET TWAIN application to control digital imaging sensor Project Description: We are seeking somebody to create a .NET TWAIN application which will activate a digital

Why is it preferred to not use finalize for clean up, The Problem with fina...

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

Enable asp.net polling, How to Enable ASP.NET polling? All our database...

How to Enable ASP.NET polling? All our database side is configured in order to get the SQL Cache working in the ASP.NET side we require to do some configuration in the web.conf

Steps of acquiring a proxy object in webservice, What are the  steps of a...

What are the  steps of acquiring a proxy object in Webservice? The following steps are required to get a proxy object of a webservice at the client side:- 1.     The Clie

Give a brief introduction on side-by-side execution, Give a brief introduct...

Give a brief introduction on side-by-side execution. Can two applications, one using private assembly and the other using the shared assembly be stated as side-by-side executables?

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd