首页 > 其他 > 详细

T4模板

时间:2014-02-08 14:54:32      阅读:446      评论:0      收藏:0      [点我收藏+]

 

T4生成块

表达式块

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".txt" #>
This file was generated: <#=System.DateTime.Now #>

表达式块

bubuko.com,布布扣
<#@ 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 ) { #>
      Theres no Bottles of Non-alcoholic Carbonated Beverage on the wall
    <# } else { #>
      Theres <#=i-1 #> Bottles of Non-alcoholic Carbonated Beverage on the wall
    <#}#>
<#}#>
bubuko.com,布布扣

使用Write和WriteLine方法

bubuko.com,布布扣
<#@ 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( "" );
} #>
bubuko.com,布布扣

声明方法

bubuko.com,布布扣
<#@ 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);
   }
#>
bubuko.com,布布扣

 

T4指令

模板指令

language :

inherits :

culture: 

输出指令

extension:

encoding:

引入指令

bubuko.com,布布扣
<# 
  var myList = new System.Collections.Generic.List<string>();
  var myDictionary = new System.Collections.Generic.Dictionary<string,System.Collections.Generic.List <string>>();
#>
bubuko.com,布布扣

 

bubuko.com,布布扣
<#@ import namespace="System.Collections.Generic" #>
<#
  var myList = new List<string>();
  var myDictionary = new Dictionary<string, List<string>>();
#>
bubuko.com,布布扣

 

包含指令

    下面的例子,引入一段类容到生成的代码文件中

‘ Copyright (c) <#=DateTime.Now.Year#>, <#=CopyrightHolder#>
‘ All rights reserved.
‘ Redistribution and use in source and binary forms, with or without
...

bubuko.com,布布扣
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".generated.cs" #>
<# var CopyrightHolder = "AdventureWorks Inc."; #>
/*
<#@ include file="License.txt" #>
*/
namespace AdventureWorks {
  // ...
}
bubuko.com,布布扣

 

数据库访问

 

bubuko.com,布布扣
<#@ 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 );
  }
#>
bubuko.com,布布扣

T4模板

原文:http://www.cnblogs.com/zhongxinWang/p/3540116.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!