2

我使用 Oleg Sych 所说的 Template - Generator 架构师通过 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" #>我也将这 3 个文件都保存为 Unicode utf8 ,但问题仍然存在,问题出在哪里?还有什么事情是我必须做的?

更新

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

4

2 回答 2

2

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

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

    public override string TransformText() 
    ...
于 2012-07-31T13:36:18.600 回答
2

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

于 2018-07-12T14:01:20.467 回答