T4模板另存为Unicode

时间:2012-07-31 11:05:33

标签: c# visual-studio-2010 templates unicode t4

我使用模板 - 生成器架构师,因为Oleg Sych说要通过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();
#>

正如Oleg Sych说的那样,我在输出中设置了Unicode,正如你在这一行中所看到的那样:<#@ output extension=".cs" encoding="UTF8" #>我也用Unicode utf8保存所有这3个文件 但问题仍然存在,问题出在哪里?还有什么东西我必须这样做?

更新

我发现我的新生成的文件(DAL.cs)都没有保存为UTF8所有这些文件都是ANSI编码保存的。需要做什么才能通过UTF8编码保存我的文件?

2 个答案:

答案 0 :(得分:2)

<#@output指令适用于T4文件的直接输出。 您需要更改模板,使其输出为UTF8

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

    public override string TransformText() 
    ...

答案 1 :(得分:0)

根据MSDN(https://msdn.microsoft.com/en-us/library/gg586943.aspx),要在output元素中执行此操作,您需要说出encoding="utf-8",所以使用连字符(字母大写似乎不会使差异)。

相关问题