当前位置:K88软件开发文章中心编程工具CodeSmith → 文章内容

CodeSmith 编写第一个代码模板

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-24 10:16:27

如AssemblyTitle,AssemblyDescription 等。对于日期部分可以通过C#代码实现如下:// Created: <%= DateTime.Now.ToLongDateString() %>可以看出来 CodeSmith 的模板文件如 AssemblyInfo.cst 和 Asp.Net 的 Page 文件中功能是非常类似,可以通过<%= 和%>直接嵌入 C# 代码(或 VB.Net,JavaScripts)。对于属性来说,可以通过先定义属性:<%@ Property Name="Author" Type="System.String" Description="Lead author of the project." %><%@ Property Name="Title" Type="System.String" Description="Title of the project." %><%@ Property Name="Description" Type="System.String" Description="Description of the project." %><%@ Property Name="Configuration" Type="System.String" Default="Debug" Description="Project configuration." %><%@ Property Name="Company" Type="System.String" Default="Guidebee Pty Ltd." %><%@ Property Name="Product" Type="System.String" Description="Product Name." %><%@ Property Name="Version" Type="System.String" Default="1.0.*" Description=".NET assembly version." %><%@ Property Name="FileVersion" Type="System.String" Default="1.0" Description="Win32 file version." %>属性定义通过 Property 定义,Name 定义属性名,Type 为属性的数据类型,Default 定义属性的缺省值, Description 可以定义属性的作用及说明。然后就可以在 C# 代码中使用这些属性,完整的代码模板如下:<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Create an AssemblyInfo.cs file." %><%@ Property Name="Author" Type="System.String" Description="Lead author of the project." %><%@ Property Name="Title" Type="System.String" Description="Title of the project." %><%@ Property Name="Description" Type="System.String" Description="Description of the project." %><%@ Property Name="Configuration" Type="System.String" Default="Debug" Description="Project configuration." %><%@ Property Name="Company" Type="System.String" Default="Guidebee Pty Ltd." %><%@ Property Name="Product" Type="System.String" Description="Product Name." %><%@ Property Name="Version" Type="System.String" Default="1.0.*" Description=".NET assembly version." %><%@ Property Name="FileVersion" Type="System.String" Default="1.0" Description="Win32 file version." %>using System.Reflection;using System.Runtime.CompilerServices;//// Created: <%= DateTime.Now.ToLongDateString() %>// Author: <%= Author %>//[assembly: AssemblyTitle("<%= Title %>")][assembly: AssemblyDescription("<%= Description %>")][assembly: AssemblyConfiguration("<%= Configuration %>")][assembly: AssemblyCompany("<%= Company %>")][assembly: AssemblyProduct("<%= Product %>")][assembly: AssemblyCopyright("Copyright (c) <%= DateTime.Now.Year.ToString() %> <%= Company %>")][assembly: AssemblyCulture("")][assembly: AssemblyVersion("<%= Version %>")][assembly: AssemblyFileVersion("<%= FileVersion %>")][assembly: AssemblyDelaySign(true)]此时如果需要“Generate output” 首先要配置代码模板的属性,这通过”Manage output” 来完成,次数如果打开 codesmith.csp 文件可以看到为 AssemblyInfo.cst 配置的属性内容:<?xml version="1.0" encoding="utf-8"?><codeSmith xmlns="http://www.codesmithtools.com/schema/csp.xsd"> <propertySets> <propertySet name="AssemblyInfo" template="AssemblyInfo.cst"> <property name="Configuration">Debug</property> <property name="Company">Guidebee Pty Ltd.</property> <property name="Version">1.0.*</property> <property name="FileVersion">1.0</property> <property name="Author">James Shen</property> <property name="Title">Code smith Demo</property> <property name="Description">My First Template code</property> <property name="Product">SandCastle</property> </propertySet> </propertySets></codeSmith>生成代码如下:using System.Reflection;using System.Runtime.CompilerServices;//// Created: Thursday, 3 January 2013// Author: James Shen//[assembly: AssemblyTitle("Code smith Demo")][assembly: AssemblyDescription("My First Template code")][assembly: AssemblyConfiguration("Debug")][assembly: AssemblyCompany("Guidebee Pty Ltd.")][assembly: AssemblyProduct("SandCastle")][assembly: AssemblyCopyright("Copyright (c) 2013 Guidebee Pty Ltd.")][assembly: AssemblyCulture("")][assembly: AssemblyVersion("1.0.*")][assembly: AssemblyFileVersion("1.0")][assembly: AssemblyDelaySign(true)]本例下载

上一页  [1] [2] 


CodeSmith 编写第一个代码模板