...
Note that this template makes use of a function GetAccessModifier and a property Accessibility, even though neither one of them is defined in the template. That's because they're defined in a separate code-behind file. Here are the contents of the code-behind file (VBCodeBehind.cst.vb):
Code Block | ||
---|---|---|
| ||
Imports System.ComponentModel Imports CodeSmith.Engine ' This class contains utility functions that can be ' used across many templates Public Class UtilityCodeTemplate Inherits CodeTemplate Private _Accessibility As AccessibilityEnum = AccessibilityEnum.Public <Category("Options"), _ Description("Accessibility of the generated class")> _ Public Property Accessibility As AccessibilityEnum Get Return _Accessibility End Get Set _Accessibility = value End Set End Property Public Enum AccessibilityEnum [Public] [Protected] [Friend] [ProtectedFriend] [Private] End Enum Public Function GetAccessModifier(ByVal accessibility As AccessibilityEnum) As String Select accessibility Case AccessibilityEnum.Public GetAccessModifier = "Public" Case AccessibilityEnum.Protected GetAccessModifier = "Protected" Case AccessibilityEnum.Friend GetAccessModifier = "Friend" Case AccessibilityEnum.ProtectedFriend GetAccessModifier = "Protected Friend" Case AccessibilityEnum.Private GetAccessModifier = "Private" Case Else GetAccessModifier = "Public" End Select End Function End Class |
The CodeTemplate
directive CodeTemplate directive ties the code-behind file to the template. The Src
attribute Src attribute of the directive specifies the filename of the code-behind file, and the Inherits
attribute Inherits attribute of the directive specifies the class in the file that the template is based on. Note that this class must itself inherit, directly or indirectly, from CodeSmith.Engine.CodeTemplate.
Because Accessibility is defined as a property of the UtilityCodeTemplate class, CodeSmith CodeSmith Generator includes it in the template's property sheet when the template is opened in CodeSmith Template Explorer:
There are two main advantages to moving code to a code-behind file. First, it :
- It makes your templates easier to understand by separating the generated code from the scripting code that drives the generation process.
...
- It makes it possible to easily reuse utility functions across many templates by moving them to shared code-behind files.
Info |
---|
Click here to download the template and source code file. |