.NET
  Home arrow .NET arrow Page 14 - Outputting Code
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Outputting Code
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 5
    2004-10-04

    Table of Contents:
  • Outputting Code
  • Understanding Script Directives
  • Understanding Other Features of the Generation Harness
  • Extending the Harness
  • Examining Code Generation Mechanics
  • Exploring Details of XSLT Code Generation
  • Creating Named Templates
  • Creating Match Templates
  • Supporting Stylesheets
  • Exploring Details of Brute-Force Generation
  • Creating a Class
  • Creating the Support Template
  • Understanding Types
  • Building a CodeDOM Graph
  • Building the Code Structure
  • Outputting Assignments
  • Creating Arrays
  • Exploring Other Features
  • Working with the CodeDOM

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More .NET Articles
    More By Apress Publishing


     

    .NET ARTICLES

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek