- Introduction
- Copying an Existing Project Template
- Updating the Template Source Code Files
- Creating a Custom User Interface for the Template
- Creating the Wizard Launch File
- Modifying the VSDir File
- Summary
Updating the Template Source Code Files
The CSharpAttrWiz subfolder called Templates contains a language version folder. For English editions of .NET, the folder is named 1033. (The name differs for other language versions of .NET you have installed.) The Templates\1033 folder contains templates that represent the basic files you need in a class library project:
Templates.inf contains a list of files that should be included with your template. The copy of Templates.inf that we created in the preceding section lists assemblyinfo.cs and file1.cs. (If you need to add more files to your template project, add each new source file to the Templates\1033 folder and then update the Templates.inf file.)
assemblyinfo.cs contains assembly-level attributes. You can customize this information for your machine; for example, to add copyright information.
file1.cs is the template source file that contains replaceable parameters and the templated source code. We'll need to modify this file to create the custom attribute class template.
Modifying the Template Source Code File
A custom attribute is defined as a class that inherits from System.Attribute and has an AttributeUsageAttribute applied to it. We'll modify the file1.cs template file to reflect these additional pieces of information.
You can use Windows Explorer to view the original CSharpDLLWiz version of file1.cs. Listing 1 shows this template source code file. (Line numbers are for reference only.) Text in bold represents template values that are replaced by the wizard engine (vsWizard.dll). These values will be replaced with a namespace and class name, respectively. I added the statement on line 8 and the inheritance clause on line 9.
Listing 1file1.cs
1: using System; 2: 3: namespace [!output SAFE_NAMESPACE_NAME] 4: { 5: /// <summary> 6: /// Summary description for [!output SAFE_CLASS_NAME]. 7: /// </summary> 8: [System.AttributeUsage( /* Add AttributeTargets here */ )] 9: public class [!output SAFE_CLASS_NAME] : System.Attribute 10: { 11: public [!output SAFE_CLASS_NAME](/* Add arguments here */) 12: { 13: // 14: // TODO: Add constructor logic here 15: // 16: } 17: } 18: }
When you select File, New, Project, Custom Attribute Library (see Figure 2), a new class library project is created, containing the wizard-generated template source code shown in Listing 2. To ensure that all of the basic details were correct, I modified the source code file to complete a custom attribute, as shown in Listing 3. The custom attribute ReferenceAttribute is defined to support adding a reference URL and the author's name. You could use this attribute to associate online reference information with source code.
Figure 2 The Custom Attribute Library applet in the New Project dialog box.
Listing 2The Wizard-Generated Custom Attribute Class
using System; namespace AttributeLibrary1 { /// <summary> /// Summary description for Class1. /// </summary> [System.AttributeUsage( /* Add AttributeTargets here */ )] public class Class1 { public Class1(/* Add positional and named arguments here */) { // // TODO: Add constructor logic here // } } }
Listing 3Custom Attribute After Modifying the Template File
using System; namespace CustomAttribute { [System.AttributeUsage(AttributeTargets.All)] public class ReferenceAttribute : Attribute { private string author; private string url; public ReferenceAttribute(string url) { this.url = url; } public string Url { get{ return url; } } public string Author { get{ return author; } set{ author = value; } } } }
We aren't quite finished. To get to the point where we can create a new custom attribute project, we need to address the default.js and common.js script files, create a wizard launching file, and add a line item to a VSDir file.
Modifying the JScript Files
The default.js file in the Scripts\1033 subfolder contains the JScript code that's used in conjunction with the wizard engine. You can customize this file when you create the new project from the template.
The common.js file in VC#Wizards\1033 contains code that's shared by all project templates. You can modify common.js if you need to call custom behaviors defined in your template's default.js script file.
Since we don't need to modify these files for our simple wizard, I won't provide the default listings here.
NOTE
Chapter 6 of my book Advanced C# Programming (Osborne/McGraw-Hill, 2002) demonstrates a project template for a wizard. This example demonstrates how to modify default.js and common.js script files.