Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

The easiest way to build a CodeSmith Generator template is to start with an example of the code that you want to generate - in this case, a finished AssemblyInfo.cs file. Here's one that we'll use as we move through this tutorial:

Code Block
languagecsharp
linenumberstrue
using System.Reflection;
using System.Runtime.CompilerServices;
//
// Created: 1/1/1973
// Author: Blake Niemyjski
//
[assembly: AssemblyTitle("User storage utility")]
[assembly: AssemblyDescription("Helps manage data in Isolated Storage files.")]
[assembly: AssemblyConfiguration("Retail")]
[assembly: AssemblyCompany("MegaUtilities, Inc.")]
[assembly: AssemblyProduct("StorageScan")]
[assembly: AssemblyCopyright("Copyright (c) MegaUtilities, Inc.")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("1.0")]
[assembly: AssemblyDelaySign(true)]

...

In the sample above, we've marked the parts decided that we want are going to automatically generate redthe Created Date on line 4, and the parts that we'll prompt the user for greento specify the values for Author, Title, Description, Configuration, Company, Product, Version and FileVersion. The rest of the file we'll treat as static text. Of course, you need to make these decisions with an understanding of how you'll use your template. In this case, for example, we've decided to hard-code the AssemblyDelaySign attribute to always be true. If your use of that attribute varied from project to project, you would want to make that a dynamic part of the template that you prompted the user for.

Now that we know what we want to build, it's time to get the content into the template.

Info
Note: Although we're not using the capability in this example, CodeSmith Generator templates can easily contain conditional logic. For example, you could prompt the user for a value for the AssemblyDelaySign attribute, and then include additional attributes in the template's output if they set that attribute to true.

...