
Using the CodeDOM
Pages: 1, 2, 3
Displaying the Generated Code
For demonstration purposes, we will use a simple .aspx file with four labels, one for each of the languages being produced.
<table width="800" border="1">
<tr>
<th>VB.NET Code</th>
</tr>
<tr >
<td>
<asp:Label ID="vbCode" Runat="server" CssClass="code">
</asp:Label>
</td>
</tr>
<tr>
<th>
C# Code</th></tr>
<tr>
<td><asp:Label ID="csharpcode" Runat="server" CssClass="code">
</asp:Label></td>
</tr>
<tr>
<th>J# Code</th></tr>
<tr >
<td>
<asp:Label ID="JSharpCode" Runat="server" CssClass="code">
</asp:Label>
</td>
</tr>
<tr>
<th>JScript.NET</th>
</tr>
<tr>
<td><asp:Label ID="JScriptCode" Runat="server" CssClass="code">
</asp:Label></td>
</tr>
</table>
In the code behind, we will instantiate an instance of the CodeDomProvider
class that we created earlier and then set the value of the code properties to
the Text
properties of the corresponding labels on the .aspx page. To make the
generated code look a little nicer on the web page, we will do some very simple
formatting to replace new lines with an HTML line break, and little spaces with
the HTML entity for a non breaking space.
private string FormatCode (string CodeToFormat)
{
string FormattedCode = Regex.Replace (CodeToFormat, "\n", "<br>");
FormattedCode = Regex.Replace (FormattedCode, " " , " ");
FormattedCode = Regex.Replace (FormattedCode, ",", ", ");
return FormattedCode;
}
Now we simply assign the generated code to the corresponding Labels
in the
.asp page.
private void Page_Load(object sender, System.EventArgs e)
{
HelloWorld.CodeDomProvider codegen = new HelloWorld.CodeDomProvider ();
vbCode.Text = FormatCode (codegen.VBCode);
csharpcode.Text = FormatCode (codegen.CSharpCode);
JScriptCode.Text = FormatCode (codegen.JScriptCode);
JSharpCode.Text = FormatCode (codegen.JSharpCode);
Page.EnableViewState = false;
}
The output should look similiar to this:
VB.NET Code
Imports System
Imports System.Text
Namespace HelloWorld
Public Class Hello_World
Public Shared Sub Main()
Dim sbMessage As System.Text.StringBuilder = _
New System.Text.StringBuilder
Dim Characters() As Char = New Char() {_
Microsoft.VisualBasic.ChrW(72), _
Microsoft.VisualBasic.ChrW(69), _
Microsoft.VisualBasic.ChrW(76), _
Microsoft.VisualBasic.ChrW(76), _
Microsoft.VisualBasic.ChrW(79), _
Microsoft.VisualBasic.ChrW(32), _
Microsoft.VisualBasic.ChrW(87), _
Microsoft.VisualBasic.ChrW(79), _
Microsoft.VisualBasic.ChrW(82), _
Microsoft.VisualBasic.ChrW(76), _
Microsoft.VisualBasic.ChrW(68)}
Dim intCharacterIndex As Integer = 0
Do While intCharacterIndex < Characters.Length
sbMessage.Append(Characters(intCharacterIndex))
intCharacterIndex = intCharacterIndex + 1
Loop
Console.WriteLine (sbMessage.ToString())
End Sub
End Class
End Namespace
C# Code
namespace HelloWorld
{
using System;
using System.Text;
public class Hello_World
{
public static void Main()
{
System.Text.StringBuilder sbMessage = new
System.Text.StringBuilder();
char[] Characters = new char[] {
'H',
'E',
'L',
'L',
'O',
' ',
'W',
'O',
'R',
'L',
'D'};
for (int intCharacterIndex = 0;
intCharacterIndex < Characters.Length;
intCharacterIndex = intCharacterIndex + 1)
{
sbMessage.Append(Characters[intCharacterIndex]);
}
Console.WriteLine (sbMessage.ToString());
}
}
}
J# Code
package HelloWorld;
import System.*;
import System.Text.*;
public class Hello_World
{
public static void main(String[] args)
{
System.Text.StringBuilder sbMessage = new
System.Text.StringBuilder();
char[] Characters = new char[]
{
'H',
'E',
'L',
'L',
'O',
' ',
'W',
'O',
'R',
'L',
'D'}
;
for (int intCharacterIndex = 0;
intCharacterIndex < Characters.Length;
intCharacterIndex = intCharacterIndex + 1)
{
sbMessage.Append(Characters[intCharacterIndex]);
}
Console.WriteLine (sbMessage.ToString());
}
}
JScript.NET Code
//@cc_on
//@set @debug(off)
import System;
import System.Text;
package HelloWorld
{
public class Hello_World
{
public static function Main()
{
var sbMessage : System.Text.StringBuilder =
new System.Text.StringBuilder();
var Characters : char[] =
['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D'];
for (var intCharacterIndex : int = 0;
; intCharacterIndex < Characters.Length;
intCharacterIndex = intCharacterIndex + 1)
{
sbMessage.Append(Characters[intCharacterIndex]);
}
Console.WriteLine (sbMessage.ToString());
}
}
}
HelloWorld.Hello_World.Main();
Conclusion
The CodeDom drives home the point that in .NET, the language is not nearly as important as the framework. This article demonstrated how to use some of the more common objects on the CodeDom that will probably be used in nearly every application of the CodeDom. The possibilities for structured code generation is really limited only by your imagination. I look forward to hearing some of the ways you are using the CodeDom.
Nick Harrison UNIX-programmer-turned-.NET-advocate currently working in Charlotte, North Carolina using .NET to solve interesting problems in the mortgage industry.
Return to ONDotnet.com
