Making the Content Dynamic
The next step is to let CodeSmith generate the parts of the output that it can calculate automatically. To do this, we'll insert C# code into our template, using special scripting tags with the same syntax as ASP.NET. CodeSmith looks for sections of your template surrounded with <%= and %> tokens, and treats the contents of those tags as expressions to evaluate at runtime. The result of those expressions is then inserted in the generated code in place of the scripting expression.
Here's the template with two expressions in place of the hard-coded dates in the original (the changes are highlighted in bold):
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Create an AssemblyInfo.cs file." %> using System.Reflection; using System.Runtime.CompilerServices; // // Created: <%= DateTime.Now.ToLongDateString() %> // 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) <%= DateTime.Now.Year.ToString() %> MegaUtilities, Inc.")] [assembly: AssemblyCulture("")] [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyFileVersion("1.0")] [assembly: AssemblyDelaySign(true)]
Now, the creation date and copyright date will be filled in automatically by CodeSmith whenever the template is executed. But there are other parts of this file that can't be determined automatically by CodeSmith, such as the assembly title and assembly description. For that sort of variable data, the solution is to prompt the user at runtime, using CodeSmith properties.