Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
What is CodeDom
What is CodeDom?
✍: Guest
“CodeDom” is an object model which represents actually a source code. It is designed to be
language independent - once you create a “CodeDom” hierarchy for a program we can then
generate the source code in any .NET compliant language. So let’s try to do something real
practical and simple to just get a feel of how powerful “CodeDom” is.
Note :- You can get the source code in CD in “CodeDom” folder.
We will try to generate the following code below. The below code which will be generated does
not do anything special buy just displays a hello message and waits for the key to be pressed.
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 in the CD has one “GenerateCode” method which returns “CodeCompileUnit” object. “CodeDom” is nothing but a full DOM model where every object in the structure represents a code unit. I have put comments the code so that the code is self understandable. I have commented 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);
Sample provided is very basic but in actual project using codedom can be very complicated. Projects where you need auto code generation codedom can be a right choice. Beware of high bulky architecture created due to codedom.
2007-10-22, 6255👍, 0💬
Popular Posts:
.NET INTERVIEW QUESTIONS - How can you avoid deadlock in threading? A good and careful planning can ...
How To Return the Second 5 Rows? - MySQL FAQs - SQL SELECT Statements with JOIN and Subqueries If yo...
How can you implement MVC pattern in ASP.NET? The main purpose using MVC pattern is to decouple the ...
How To Give a User Read-Only Access to a Database? - MySQL FAQs - Managing User Accounts and Access ...
How did you implement UML in your project ? PART II Implementation phase / Coding phase (Class diagr...