.NET
  Home arrow .NET arrow Page 15 - 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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 the Code Structure


    (Page 15 of 19 )

    Before using these declarations, take a step back and look at the structure of the method that builds the CodeDOM graph. The compile unit contains namespaces, namespaces contain types (a class in this case), and types contain members (a method in this case) (See footnote 9.):

    Dim nSpace As New CodeNamespace("CodeDOMTest")
    compileUnit.Namespaces.Add(nSpace)
    ' Create a class to hold code
    Dim clsStartup As New CodeTypeDeclaration("Startup")
    nSpace.Types.Add(clsStartup)
    ' To run as an executable, you'll need a method that's an entry point
    Dim entry As New CodeEntryPointMethod
    entry.Name = "Main"
    clsStartup.Members.Add(entry)

    If you don’t want a namespace declared in your output, just use an empty string for the namespace name. You always have to include the namespace object, but if it doesn’t have an explicit name, it isn’t output.

    Once you’ve created an object to represent the method that the CodeDOMProvider will later output, you can stuff code into it. Code consists of statements. You create an object representing each method and add to its Statements collection. Statements will be output in the order they appear in the Statements collection (See footnote 10):

    entry.Statements.Add(decl)
    entry.Statements.Add(decl2)
    entry.Statements.Add(decl3)
    entry.Statements.Add(decl4)

    Footnote 9. The code in this section is cumulative; compileUnit was declared earlier in the section “Building the CodeDOM Graph.”

    Footnote 10. decl1, decl2, decl3, and decl4 were declared earlier in the section “Declaring Variables.”

    Outputting Statements

    The next code creates a reference to the system console class and outputs one of the primitive expressions defined earlier. This is similar to code in the “Hello World” example in Chapter 1:

    Dim rExpConsole As New CodeTypeReferenceExpression( _
    GetType(System.Console))
    Dim stmt1 As New CodeExpressionStatement( _
    New CodeMethodInvokeExpression(rExpConsole, _
    "WriteLine", exp))
    entry.Statements.Add(stmt1)
     


    Similarly Named Classes Can Be Confusing

    Working with types is sometimes confusing. Types are generally, but not always, classes. The word GetType is a keyword and a method, and different .NET Framework classes sometimes have similar names. Some of the classes with the most confusing names support the CodeDOM and the automation model of EnvDTE. To keep these straight, watch the namespace with which you’re working. For example:

    • CodeTypeReference: Inherits from CodeObject and contains a reference to a type. Use this within other CodeDOM objects to declare variables, parameters, and so on.

    • CodeTypeReferenceExpression: Inherits from CodeExpression. Use this to call static (Shared) methods.

    • CodeTypeRef: Part of the Visual Studio .NET automation model and not used in CodeDOM programming.

    CodeTypeReference and CodeTypeReferenceExpression are in the System.CodeDOM namespace. CodeTypeRef is in the EnvDTE namespace. Help lists CodeTypeRef and other members of the EnvDTE namespace as CodeTypeRef objects instead of the more common listing as a class entry.


    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

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT