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

Delegate, What is a delegate? The Delegate is the class that can hold a...

What is a delegate? The Delegate is the class that can hold a reference to a method or a function. The Delegate class has a signature and it can only reference to those methods

What is dynamic language runtime, What is Dynamic Language Runtime (DLR)? ...

What is Dynamic Language Runtime (DLR)? DLR is a runtime environment that permits you to integrate dynamic languages with the Common Language Runtime (CLR) by adding a set of s

Various objects in dataset, What are the various objects in Dataset? Th...

What are the various objects in Dataset? The Dataset has a collection of DataTable object inside the Tables collection. Every DataTable object have  a collection of DataRow obj

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

Create custom controls in asp.net, How can we create custom controls in ASP...

How can we create custom controls in ASP.NET? The User controls are created using .ASCX in ASP.NET. After that .ASCX file is created you need two things in order to that the AS

What happens when aspx page is requested from browser ?, The steps that occ...

The steps that occur when we request a ASPX page are as follows:- 1) The browser sends the request to the webserver. Now  assume that the webserver at the other end is IIS. 2

Sequence of uml diagrams in project, What is the sequence of UML diagrams i...

What is the sequence of UML diagrams in project? At First let me say some fact about this question, you cannot implement all the nine diagrams given by the UML in one project;

Difference between class and structure, What is the difference between Clas...

What is the difference between Class and structure's? The main differences between them are as follows:- 1) The Structure is value types while classes are referen

Name the binders provided by .net framework 4.0, Name the binders provided ...

Name the binders provided by .NET Framework 4.0. .NET Framework 4.0 provides the following binders: Object Binder - Enables to communicate with .NET objects. JavaScrip

Program of nested loop - c# program, Program of Nested Loop - C# Program ...

Program of Nested Loop - C# Program I am a learner of C# language and i am struggling with the Loops in c#. Can you have any code examples for this.

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