Introducing Code Generation - Generating a Simple Program
(Page 2 of 13 )
Looking at a “Hello World” program in each of the three primary code generation mechanisms lets you focus on the similarities and differences in the mechanisms themselves. Listing 1-1 shows the target “Hello World” program.
Listing 1-1. A Target “Hello World” Program for a Preliminary Look at Code Generation
Option Strict On
Option Explicit On
Imports System
' Class Summary: Hello World target output
Public Class TargetHelloWorld
#Region "Public Methods and Properties"
Public Shared Sub Main()
Console.WriteLine("Hello World")
End Sub
#End Region
End Class
In the real world, this isn’t a good candidate for code generation because it isn’t a pattern applied with predictable variations. It’s always the same, so it’s easier to write this “Hello World” program without code generation than with it. But the goal here is to strip code generation down to its naked essentials to illustrate the underlying process.
***************
NOTE The code you can download for this book (in C# and VB .NET) will include the best practices I use. In VB .NET I always use Option Strict On and Option Explicit On for strict typing. In VB .NET, I include Imports System and limit other imported namespaces to leave part of the namespace in each class reference. Imports System provides access to the system data type names, which I prefer for clarity. I also heavily use regions. Regions make it easier to trace between the output code and the emitting code while you’re debugging your templates. I’ll rarely use the VB library, which, among other things, makes it easier for C# developers to read my code. I’ll avoid setting options or imports at the project level, preferring that each file be explicit and self-contained. These details aren’t always evident in the code fragments printed in the book. You can assume that all VB .NET code is strictly typed (Option Strict and Option Explicit both set to On).
****************
TIP Specify code options in generated code, and don’t rely on project settings. You can’t be sure of what project setting will be in use in the generated application.
Generating “Hello World” via Brute Force The obvious approach to code generation is to create a stream writer and start outputting code. The big benefit of this method is that it requires only a basic understanding of writers and streams, which you may already have. The vast majority of programmers I’ve talked to who have experimented with code generation have used this approach. Later you’ll see one way to organize your templates to make this option easier. Brute-force code generation doesn’t impose the same level of organization on you that XSLT code generation does, but with some discipline you can manually supply this same high level of organization, and it’ll benefit your code generation process.
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: Creating the Template >>
More .NET Articles
More By Apress Publishing