Outputting Code - Building a CodeDOM Graph
(Page 14 of 19 )
To show how to build CodeDOM templates, I’ll start with CodeDOM code that creates literals in your output code. Then I’ll show how to create the structure of the CodeDOM graph and finally how to hook them together with CodeDOM code that creates method and class objects that later create your application code. The code in this section is cumulative. For example, I declare the CodeCompileUnit here and use it throughout later sections of the chapter. The compile unit will contain the CodeDOM graph that holds the rest of the code specifics. You declare the compile unit that contains the CodeDOM graph with this:
Dim compileUnit As New CodeDom.CodeCompileUnit
You enter expressions, such as primitive expressions, using the syntax of the language you’re using when you create the CodeDOM graph—in this case, VB .NET. The code you output depends on the CodeDOM provider you’re using.
Using CodePrimitiveExpressions The most basic type of expression is a CodePrimitiveExpression. You can’t just say “Hello World” or 42 with the CodeDOM. You have to transform everything, including literals, into an object. A few examples of primitive expressions are as follows:
Dim exp As New CodePrimitiveExpression("Hello World")
Dim exp2 As New CodePrimitiveExpression(42)
Dim exp3 As New CodePrimitiveExpression(3)
Dim s As String = "Sam"
Dim exp4 As New CodePrimitiveExpression(s)
Dim exp5 As New CodePrimitiveExpression(True)
Dim exp6 As New CodePrimitiveExpression(Nothing)
Primitives are literals—a basic unit of programming. It’s impossible to imagine any nontrivial application that doesn’t use literals. Although it might initially look confusing, it’s perfectly legal to use a variable to define the value of a CodePrimitiveExpression, such as the definition of the literal "Sam".
The previous examples that create primitives for True and Nothing may be the most interesting. They illustrate the primitive expression entered in the syntax of the language you’re using to create the CodeDOM graph—in this case, VB .NET. When this value is output in the target language, it may not appear the same. Regardless of the language you use to create the CodeDOM template, if the target language is C#, the code provider outputs true (lowercase) and null. If the target language is VB, the code provider outputs True (mixed case) and Nothing.
Declaring Variables Another basic type of expression is a local variable declaration. To produce code that will compile, you’ll have to declare local variables before you use them, as either a variable or a parameter. Examples of variable declarations with and without initialization are as follows:
' Declare an integer variable named iSum
Dim decl As New CodeVariableDeclarationStatement( _
GetType(System.Int32), "iSum")
' Declare an integer variable named iValue initialized to the expression Exp2
Dim decl2 As New CodeVariableDeclarationStatement( _
"System.Int32", "iValue", exp2)
' Declare an object of the stream class named stream
Dim decl3 As New CodeVariableDeclarationStatement( _
GetType(System.IO.Stream), "stream")
' Declare a string variable named fileName with the value Test.txt
Dim decl4 As New CodeVariableDeclarationStatement( _
GetType(System.String), "fileName", _
New CodePrimitiveExpression("Test.txt"))
Within the CodeVariableDeclarationStatement, you can declare the type of a variable either using the type (GetType is the VB equivalent of the C# typeof operator) or via a string. If you declare types using a string, you lose IntelliSense and compiler support in catching your typing mistakes. You can avoid using strings when you’re working with framework types, but sometimes you’re referencing unavailable types, such as referencing types in the code you’re generating, and you’ll have to use a string. If a third parameter is supplied, it’s used to initialize the variable.
You create references to variables using the CodeVariableReferenceExpression method shown later.
NOTE: The name CodeVariableReferenceExpression has nothing to do with whether the underlying variable represents a value type or a reference type. It’s just the CodeDOM way of referencing a variable. |
This chapter is from Code Generation in Microsoft .NET by Kathleen Dollard (Apress, 2004, ISBN: 1590591372). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Building the Code Structure >>
More .NET Articles
More By Apress Publishing