2012-07-31 85 views
2

我使用模板生成器架構師,因爲Oleg Sych表示通過T4生成一些代碼,但在使用Unicode時存在一些問題,生成所有顯示爲問號的波斯語字符後我有一些波斯字符。T4模板保存爲Unicode

這是我的文件:模板:

<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #> 
<#@ assembly name="Microsoft.SqlServer.Smo" #> 
<#@ assembly name="Microsoft.SqlServer.Management.Sdk.Sfc" #> 
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #> 
<#@ import namespace="System.Collections.Generic" #> 
<#+ 
public class DALTable : Template 
{ 

    public override string TransformText() 
    { 

     //My Template 

    } 

發電機:

<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #> 
<#@ assembly name="Microsoft.SqlServer.Smo" #> 
<#@ assembly name="Microsoft.SqlServer.Management.Sdk.Sfc" #> 
<#@ include file="DALTable.tt" #> 
<#+ 
public class TableDALGenerator : Generator 
{ 
    public DALTable DALTableTemplate = new DALTable(); 

    protected override void RunCore() 
{ 
     this.DALTableTemplate.Table = table; 
     this.DALTableTemplate.RenderToFile("DAL.cs"); 
    } 
} 

和文字:

<#@ template language="C#v4" hostspecific="True" debug="True" #> 
<#@ assembly name="System.Core" #> 
<#@ output extension=".cs" encoding="UTF8" #> 
<#@ include file="T4Toolbox.tt" #> 
<#@ include file="TableDALGenerator.tt" #> 
<# 
    TableDALGenerator TableDALGen = new TableDALGenerator(); 
    //Pass Parameters 
    TableORMGen.Run(); 
#> 

由於奧列格Sych發表說我在輸出設置Unicode作爲你在看此行:<#@ output extension=".cs" encoding="UTF8" #>另外我還保存了所有這3個帶有Unicode的文件utf8 但問題仍然存在,問題在哪裏?我必須做的還有什麼呢?

更新

我發現沒有我的新生成的文件(DAL.cs)不保存爲UTF8所有的人都救了ANSI編碼。需要做什麼來保存UTF8編碼的文件?

回答

2

<#@output指令適用於T4文件的直接輸出。 你需要改變你的模板,它的輸出是UTF8

public class DALTable : Template 
{ 
    public DALTable() 
    { 
     Output.Encoding = System.Text.Encoding.UTF8; 
    } 

    public override string TransformText() 
    ... 
+0

我在這裏提出一個相關的問題:http://stackoverflow.com/questions/13838142/put-all-methods-in-one-tt-file在另一個t4文件在代碼模板中,請你檢查一下 – Saeid 2012-12-12 11:15:06