Outputting Code - Creating Arrays
(Page 17 of 19 )
You create arrays that can be assigned to previously declared array variables using the CodeArrayCreateExpression:
entry.Statements.Add(New CodeAssignStatement( _
New CodeVariableReferenceExpression("a2Ints"), _
New CodeArrayCreateExpression("System.Int32", 10)))
Dim varAInts As New CodeVariableReferenceExpression("aInts")
entry.Statements.Add(New CodeAssignStatement( _
varAInts, _
New CodeArrayCreateExpression("System.Int32", _
New CodePrimitiveExpression(0), _
New CodePrimitiveExpression(1), _
' Additional assignments snipped
New CodePrimitiveExpression(9))))
Assigning Array Values Accessing individual values within the array requires a CodeArrayIndexerExpression. This creates output such as aInts(iValue) in VB .NET or aInts[iValue] in C#. The next example assigns the fourth (see footnote 12) element in the array to the previously declared variable named iValue:
entry.Statements.Add( _
New CodeAssignStatement( _
New CodeVariableReferenceExpression("iValue"), _
New CodeArrayIndexerExpression( _
varAInts, New CodePrimitiveExpression(3))))
Footnote 12. .NET arrays are zero based.
Okay, enough simple stuff. Let’s look at something a little more complex. What would it take to output the following code?
Dim i as Integer
aInts(i) = aInts(i + 1)
After declaring the variable i, create an expression variable pointing to i. This and the previously created expression variable pointing to the array save some typing:
entry.Statements.Add( _
New CodeVariableDeclarationStatement( _
"System.Int32", "i", _
New CodePrimitiveExpression(0)))
Dim varI As New CodeVariableReferenceExpression("i")
Outputting the target code requires array indexers for both the left and right operands of the assign statement. The left operand uses the variable reference expression pointing to the array and the variable reference expression pointing to i. The right operand also uses the variable reference expression for the array. For the array index, it uses a binary operator expression and the addition operator. The left operand of this binary operator expression is the variable reference expression for i, and the right is a primitive expression for 1:
entry.Statements.Add( _
New CodeAssignStatement( _
New CodeArrayIndexerExpression( _
varAInts, varI), _
New CodeArrayIndexerExpression( _
varAInts, _
New CodeBinaryOperatorExpression( _
varI, CodeBinaryOperatorType.Add, _
New CodePrimitiveExpression(1)))))
This doesn’t output a particularly complex line of code. It outputs a short, rather ordinary line of code, and the CodeDOM template is beginning to feel convoluted. If you understand how this piece of code works, though, you’ll be able to extend it to more complex code statements on your own.
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: Exploring Other Features >>
More .NET Articles
More By Apress Publishing