CodeDOM
The CodeDOM is .NET's mechanism for generating code at runtime. The CodeDOM
exposes most of the .NET language features. Using the CodeDOM you can generate
source code in any .NET language that provides a code provider. Using the
CodeDOM you can generate either source code files or assemblies.
The CodeDOM is used in .NET to generate such things as ASP.NET themes, and ASP.NET
pages. In reality any time you need to generate an assembly you can do so
in the CodeDOM although other techniques such as templates and XAML may be more
appropriate.
FAQs
Q: How can I generate a region around a block of type members? (Forums) Answer
A: The important thing to remember about a region in the CodeDOM is that it is associated
with a member through the StartDirectives or EndDirectives
property. Start directives appear before the member while end directives appear
after. 
There is no requirement that a directive both start and end on the same member.
Therefore to create a region (which is a code directive) you set it as a start directive
on the first member and as the end directive on the last member.
CodeMemberMethod meth = new CodeMemberMethod();
meth.Name = "Foo";
meth.StartDirectives.Add(
new CodeRegionDirective(CodeRegionMode.Start, "Methods"));
cls.Members.Add(meth);
meth = new CodeMemberMethod();
meth.Name = "Foo2";
meth.EndDirective.Add(
new CodeRegionDirective(CodeRegionMode.End, ""));
cls.Members.Add(meth);
The above code will generate a region containing the methods Foo and Foo2.
Q: How can I generate a comment within a type definition? (Forums)
Answer
A: This is not so obvious. Types can only have type members and a comment
is represented by the CodeCommentStatement class which is a statement.
Members themselves have the Comments property where you can attach
the comments but the comments will appear above the member.
The workaround is to make the statement a type member instead. Enter CodeSnippetTypeMember.
This class is generally used to include raw snippets of text into a class as a type
member. However it also opens the door to attaching a comment to the type.
There is no requirement that a snippet type member has any snippet code to generate.
Therefore create an empty snippet type member and attach the comment to it like
so.
CodeSnippetTypeMember clsComment = new CodeSnippetTypeMember();
clsComment.Comments.Add(new CodeCommentStatement("Testing"));
cls.Members.Add(clsComment);
Q: How can I generate a using statement? (Forums)
Answer
A: A using statement is not language independent.
Therefore you can not generate it through the CodeDOM. Instead you have to
generate the equivalent code. A using statement boils down
to this code (assuming that the object implements IDisposable).
IDisposable value = null;
try
{
value = new ...;
} finally
{
if (value != null)
value.Dispose();
};
Generate the above code using the CodeDOM to create an equivalent to the using statement.
Q: How can I generate a while statement? (Forums)
Answer
A: It depends. Both the for and the
while loop are generated using the same block of code. Everybody
knows that a for loop can be implemented as a while
loop. However the inverse also applies. In VB a for
and while loop are generated as Do While statements.
However in C# the for and while loop are always
generated as a for loop.
To cut down on the amount of code that is generated you can set the InitializeStatement
and the IncrementStatement properties to an empty statement to
avoid the extra code generation. Note that I said an empty statement, not
a null statement. Setting either property to null will cause
an exception.
Q: How can I generate an empty statement?
Answer
A: An empty statement is not equivalent to a null statement.
An empty statement is a statement that has no content. Surprisingly there
is no direct way to do this. However you can easily generate an empty statement
by using the CodeSnippetStatement and specifying an empty string.
This will generate an empty statement.