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. |
Next: Outputting Assignments >>
More .NET Articles
More By Apress Publishing