<#@ template debug="false" hostspecific="false" language="C#" #> <#@ output extension=".txt" #> This file was generated: <#=System.DateTime.Now #>
<#@ template debug="false" hostspecific="false" language="C#" #> <#@ output extension=".txt" #> <# for( int i=5; i>=1; i=i-1){ #> <#= i #> Bottles of Non-alcoholic Carbonated Beverage on the wall <#= i #> Bottles of Non-alcoholic Carbonated Beverage Take one down And pass it around <# if( i-1 == 0 ) { #> There‘s no Bottles of Non-alcoholic Carbonated Beverage on the wall <# } else { #> There‘s <#=i-1 #> Bottles of Non-alcoholic Carbonated Beverage on the wall <#}#> <#}#>
<#@ template debug="false" hostspecific="false" language="C#" #> <#@ output extension=".txt" #> <# for( int i = 99; i > 1; i-- ) { WriteLine( "{0} Bottles of Non-alcoholic Carbonated Beverage on the wall", i); WriteLine( "{0} Bottles of Non-alcoholic Carbonated Beverage", i ); WriteLine( "Take one down" ); WriteLine( "And pass it around" ); if( i - 1 == 0 ) { WriteLine( "There‘s no Bottles of Non-alcoholic Carbonated Beverage on the wall." ); } else { WriteLine( "There‘s {0} Bottles of Non-alcoholic Carbonated Beverage on the wall.",i-1); } WriteLine( "" ); } #>
<#@ template debug="false" hostspecific="false" language="C#" #> <#@ output extension=".txt" #> Financial Sample Data <# for( int i = -5; i <= 5; i++ ) { WriteFinancialNumber(i); WriteLine( "" ); } #> End of Sample Data <#+ void WriteFinancialNumber(decimal amount) { if( amount < 0 ) Write("(${0:#0.00})", System.Math.Abs(amount) ); else Write("${0:#0.00}", amount); } #>
language :
inherits :
culture:
extension:
encoding:
<# var myList = new System.Collections.Generic.List<string>(); var myDictionary = new System.Collections.Generic.Dictionary<string,System.Collections.Generic.List <string>>(); #>
<#@ import namespace="System.Collections.Generic" #> <# var myList = new List<string>(); var myDictionary = new Dictionary<string, List<string>>(); #>
下面的例子,引入一段类容到生成的代码文件中
‘ Copyright (c) <#=DateTime.Now.Year#>, <#=CopyrightHolder#>
‘
All rights reserved.
‘ Redistribution and use in source and binary forms,
with or without
...
<#@ template debug="false" hostspecific="false" language="C#" #> <#@ output extension=".generated.cs" #> <# var CopyrightHolder = "AdventureWorks Inc."; #> /* <#@ include file="License.txt" #> */ namespace AdventureWorks { // ... }
<#@ template debug="false" hostspecific="false" language="C#" #> <#@ output extension=".generated.cs" #> <#@ assembly name="System.Data" #> <#@ import namespace="System.Data.SqlClient" #> <#@ import namespace="System.Text.RegularExpressions" #> <# var connectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=true;"; var sqlString = "SELECT ContactTypeID, [Name] FROM [Person].[ContactType]"; #> // This code is generated. Please do not edit it directly // If you need to make changes please edit ContactType.tt instead namespace AdventureWorks { public enum ContactType { <# using(var conn = new SqlConnection(connectionString)) using(var cmd = new SqlCommand(sqlString, conn)) { conn.Open(); var contactTypes = cmd.ExecuteReader(); while( contactTypes.Read() ) { #> <#= ValidIdentifier( contactTypes[1].ToString() ) #> = <#=contactTypes[0]#>, <#} conn.Close(); } #> } } <#+ public string ValidIdentifier(string input) { return Regex.Replace(input, @"[^a-zA-Z0-9]", String.Empty ); } #>
原文:http://www.cnblogs.com/zhongxinWang/p/3540116.html