一 “Hello World ”代碼生成器 1 創(chuàng)建一個(gè)C# Console工程,,添加一個(gè)名為“HelloWorld.tt”的文本文件。
<#@ template language="C#" #> // <autogenerated> // This code was generated by a tool. Any changes made manually will be lost // the next time this code is regenerated. // </autogenerated> using System; public class <#= this.ClassName #> { public static void HelloPot() { Console.WriteLine("Hello World"); } } 上面的模板將生成一個(gè)名為“HelloWorld”的類,當(dāng)你保存HelloWorld.tt時(shí),,Visual Studio將為你生成以下代碼: // <autogenerated> // This code was generated by a tool. Any changes made manually will be lost // the next time this code is regenerated. // </autogenerated> using System; public class HelloWorld { public static void HelloPot() { Console.WriteLine("Hello World"); } } 3 添加另一個(gè)文本文件HelloWorld1.tt.,加入以下內(nèi)容: <# this.ClassName = "HelloWorld1"; #> <#@ include file="HelloT4.tt" #> 通過(guò)修改ClassName的值可以更改類名,以上模板將生成一個(gè)名為HelloWorld1類,。
// <autogenerated> // This code was generated by a tool. Any changes made manually will be lost // the next time this code is regenerated. // </autogenerated> using System; public class HelloWorld1 { public static void HelloPot() { Console.WriteLine("Hello World"); } } 二 數(shù)據(jù)庫(kù)自動(dòng)生成實(shí)體類 這個(gè)實(shí)例會(huì)創(chuàng)建一個(gè)模板為數(shù)據(jù)庫(kù)中的每一張表,,自動(dòng)創(chuàng)建相應(yīng)的實(shí)體類。 1 首先我們需要添加一些程序集引用,,命名空間,。
<#@ template language="C#" debug="True" hostspecific="True" #> <#@ output extension=".cs" #> <#@ assembly name="System.Data" #> <#@ assembly name="System.xml" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="System.Data.SqlClient" #> <#@ import namespace="System.Data" #> <# string connectionString = "data source=.""SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=MIAPortal;"; SqlConnection conn = new SqlConnection(connectionString); conn.Open(); System.Data.DataTable schema = conn.GetSchema("TABLES"); string selectQuery = "select * from @tableName"; SqlCommand command = new SqlCommand(selectQuery,conn); SqlDataAdapter ad = new SqlDataAdapter(command); System.Data.DataSet ds = new DataSet();
<#@ template language="C#" debug="True" hostspecific="True" #> <#@ output extension=".cs" #> <#@ assembly name="System.Data" #>
<#@ assembly name="System.xml" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="System.Data.SqlClient" #> <#@ import namespace="System.Data" #> using System; namespace MyProject.Entities { <# string connectionString = "data source=.""SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=MIAPortal;"; SqlConnection conn = new SqlConnection(connectionString); conn.Open(); System.Data.DataTable schema = conn.GetSchema("TABLES"); string selectQuery = "select * from @tableName"; SqlCommand command = new SqlCommand(selectQuery,conn); SqlDataAdapter ad = new SqlDataAdapter(command); System.Data.DataSet ds = new DataSet(); foreach(System.Data.DataRow row in schema.Rows) { #> public class <#= row["TABLE_NAME"].ToString().Trim('s') #> { <# ds.Tables.Clear(); command.CommandText = selectQuery.Replace("@tableName",row["TABLE_NAME"].ToString()); ad.FillSchema(ds, SchemaType.Mapped, row["TABLE_NAME"].ToString()); foreach (DataColumn dc in ds.Tables[0].Columns) { #> private <#= dc.DataType.Name #> _<#= dc.ColumnName.Replace(dc.ColumnName[0].ToString(), dc.ColumnName[0].ToString().ToLower()) #>; public <#= dc.DataType.Name #> <#= dc.ColumnName #> { get { return _<#= dc.ColumnName.Replace(dc.ColumnName[0].ToString(), dc.ColumnName[0].ToString().ToLower()) #>; } set { _<#= dc.ColumnName.Replace(dc.ColumnName[0].ToString(), dc.ColumnName[0].ToString().ToLower()) #> = value; } } <# } #> } <# } #> }
How to create a simple T4 template---Oleg Sych
|
|
來(lái)自: ThinkTank_引擎 > 《T4》