1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

The CodeDOM and the Delphi for .NET IDE
The Code Document Object Model (CodeDOM) allows .NET developers to generate and compile source code at run time in a variety of languages. This article provides an overview of the CodeDOM.
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• Part 2: Creating a CodeDOM in Delphi Code
• Part 3: DelphiProvider - a CodeDomProvider for Delphi developers

 Join the Discussion
"Post your views, comments, questions and doubts to this article."
 Related Resources
• Delphi for .NET topics
 Elsewhere on the Web
• CodeDOM on MSDN
• CodeDOM Quick Reference

Article originally written by Corbin Dunn (Borland R&D Software Engineer).

What is the CodeDOM?
The Code Document Object Model (CodeDOM) allows .NET developers to generate and compile source code at run time in a variety of languages. The CodeDOM is a collection of classes used to represent source code. Once source code is represented in a CodeDOM, it can then be printed, compiled to an assembly, or compiled to memory and executed. An application can also use the CodeDOM as an abstract layer for reading source code without having intrinsic knowledge of the underlying language.

The WinForms / WebForms Designers
The most predominant uses of the CodeDOM are in the WinForms and WebForms designers of the .NET Framework. These designers require a CodeDOM to be provided for them, and walk the DOM looking for types that can be designed. Once a type is found, they look for the InitializeComponents method and walk each statement in the method, executing them as it does along. The end result is a deserialized form (or web) designer.

When changes are made to the form designer, the current state of the designer is flushed out to a CodeDOM object. This object can then be injected back into the original source code. By using an abstract set of classes, the WinForms and WebForms designers do not need to know anything about the underlying programming language.

ASP.NET also uses the CodeDOM. When ASPX pages have source code mixed in, the ASP.NET engine must some how compile this source. One of the underlying problems stems from the fact that someone can write an ASPX page in a variety of different languages. In order to keep it well abstracted, the ASP.NET engine creates a CodeDOM for the page. The CodeDOM is language independent, making the engine able to manipulate any language that has a CodeDOM provider. It can then compile the CodeDOM to an assembly, execute it, and return the resulting HTML.

CodeDOM Basics
Using the CodeDOM is fairly straightforward. To get a better grasp of it, I highly recommend reading the help: Generating and compiling source code dynamically in multiple languages. Plus, the CodeDOM Quick Reference is indispensable.

General CodeDOM Overview
Contains everything and is the top level CodeDOM object. It primarily contains one or more CodeNamespace objects.

Represents a namespace in source code. The namespace can contain types that are in that namespace. Each type is a CodeTypeDeclaration or a descendant thereof.

The basic CodeTypeDeclaration is used to represent classes, enumerations, interfaces, and structures (records). A CodeTypeDelegate is used to represent a delegate (which is an event declaration). The CodeTypeDeclaration contains CodeTypeMembers to represent the members in the type.

This is the base abstract class for all type members. There are things to represent all basic code members, including: CodeMemberMethod, CodeMemberField, CodeMemberProperty, CodeConstructor, and a CodeTypeConstructor (a class constructor). Most CodeTypeMembers can have attributes applied to them; this is done via a CodeAttributeDeclaration, which, unlike most all the other classes, does not descend from CodeObject.

This class allows you to specify an attribute. It basically contains the name of the attribute, and any arguments passed to it (which are also represented by CodeDOM objects).

The CodeMemberMethod contains Statements each of which are an instance of a CodeStatement object.

The CodeStatement is the base class for all statements. There are CodeAssignStatement, CodeConditionStatement (used for if or while) along with many others. Each statement is generally made of up expressions (or it is an expression, with the CodeExpressionStatement), represented by the CodeExpression abstract class.

Represents all expressions. Expressions include ways of referencing fields, properties, and variables (CodeFieldReferenceExpression, CodePropertyReferenceExpression, and CodeVariableReferenceExpression), along with ways of invoking events, or referencing primitive values (such as integers, or strings).

The CodeSnippetXXX classes (such as CodeSnippetExpression) are useful for representing a snippet of source code that cannot be represented in the CodeDOM. The problem with snippets is that they are language dependent.

The CodeObject class is the base abstract class for most all of the other CodeDOM classes. The main thing that it introduces is the UserData property; allowing a user of the CodeDOM to store information in a particular CodeObject.

But what can you do with CodeDOM objects once you have created them? The System.CodeDom.Compiler namespace has most of the classes and interfaces used to do things with a CodeDOM.

One thing you may want to do, is print the CodeDOM in a particular language. To do this, you must get a code generator (ICodeGenerator) for that particular language. An ICodeGenerator is generally acquired through a CodeDomProvider. A CodeDomProvider also allows you to create a code compiler (ICodeCompiler) to compile the code.

For example of this, see the included source code.

We've done with the theory, let's move to Delphi code...

Next part > Creating a CodeDOM in Delphi Code > Part 1, 2, 3

©2016 About.com. All rights reserved.