如何在 T4 模板中使用 C# v6 的最新功能?
How can I use latest features of C# v6 in T4 templates?
我正在尝试 运行 2015 年 Visual Studio 的新 T4 模板。但是它无法在这一行编译:
var message = $"Linked table '{linkedTable}' does not exist.";
编译器报告“$”字符是意外的。但是,根据新的 string interpolation 功能指南,此语法在 C# v6 中应该有效。
有没有办法让 T4 模板引擎使用更新的 C# 版本,除了在外部库中编译我的代码?
更新:
文件的声明元素如下,供参考:
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".sql" #>
<#@ SqlModelDirective processor="SqlModelDirectiveProcessor" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="Microsoft.SqlServer.Dac" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="Microsoft.SqlServer.Dac.Model" #>
除了 SqlModelDirective
元素之外,我认为这是非常标准的。
您现在不能在 T4 模板中使用 C# 6,因为它们不使用最新的编译器。
您可以在 template 指令的 compilerOption
属性中指定编译器选项。如果使用最新的编译器,你可以使用:
<#@ template debug="false" hostspecific="false" language="C#"
compilerOptions="/langversion:6" #>
当我尝试这样做时,出现以下错误:
Compiling transformation: Invalid option '6' for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default
更新
在 Github 的 ASP.NET 存储库中找到 this discussion。似乎 ASP.NET 团队正在寻求用 Razor 替换 T4(顺便说一句,不是一个坏主意)。想知道@davidfowl 是否有更多信息 ;)
更新 2
David Fowler responded on Twitter - T4 uses CodeDom which hasn't been updated to use Roslyn yet. While there is a NuGet package with replacement CodeDom providers,它仅适用于 ASP.NET 4.x 项目,不适用于 T4。
所以目前 T4 中没有 C# 6。
您应该升级到 2016 年 3 月 30 日发布的 Visual Studio 2015 Update 2,它引入了此类功能。在其“其他变化”下:
Enhanced T4 text templates so that they now support C# 6.0.
但是,该功能在 Visual Studio 2015 更新 3 中再次被破坏。
在 Visual Studio 2017 年(也可能是 2015 年),将最新的 Microsoft.Net.Compilers
nuget 包添加到包含 T4 模板的项目将启用最新的 C# 功能,例如内插字符串。 (我刚刚使用 Visual Studio 2017 15.6.2 和软件包的 2.7.0 版完成了此操作)。
这在 Visual Studio 2019 年对我不起作用,所以我求助于创建一个单独的项目,我用汇编指令引用了该项目。本质上,程序集是一个 class 功能块
我正在尝试 运行 2015 年 Visual Studio 的新 T4 模板。但是它无法在这一行编译:
var message = $"Linked table '{linkedTable}' does not exist.";
编译器报告“$”字符是意外的。但是,根据新的 string interpolation 功能指南,此语法在 C# v6 中应该有效。
有没有办法让 T4 模板引擎使用更新的 C# 版本,除了在外部库中编译我的代码?
更新:
文件的声明元素如下,供参考:
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".sql" #>
<#@ SqlModelDirective processor="SqlModelDirectiveProcessor" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="Microsoft.SqlServer.Dac" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="Microsoft.SqlServer.Dac.Model" #>
除了 SqlModelDirective
元素之外,我认为这是非常标准的。
您现在不能在 T4 模板中使用 C# 6,因为它们不使用最新的编译器。
您可以在 template 指令的 compilerOption
属性中指定编译器选项。如果使用最新的编译器,你可以使用:
<#@ template debug="false" hostspecific="false" language="C#"
compilerOptions="/langversion:6" #>
当我尝试这样做时,出现以下错误:
Compiling transformation: Invalid option '6' for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default
更新
在 Github 的 ASP.NET 存储库中找到 this discussion。似乎 ASP.NET 团队正在寻求用 Razor 替换 T4(顺便说一句,不是一个坏主意)。想知道@davidfowl 是否有更多信息 ;)
更新 2
David Fowler responded on Twitter - T4 uses CodeDom which hasn't been updated to use Roslyn yet. While there is a NuGet package with replacement CodeDom providers,它仅适用于 ASP.NET 4.x 项目,不适用于 T4。
所以目前 T4 中没有 C# 6。
您应该升级到 2016 年 3 月 30 日发布的 Visual Studio 2015 Update 2,它引入了此类功能。在其“其他变化”下:
Enhanced T4 text templates so that they now support C# 6.0.
但是,该功能在 Visual Studio 2015 更新 3 中再次被破坏。
在 Visual Studio 2017 年(也可能是 2015 年),将最新的 Microsoft.Net.Compilers
nuget 包添加到包含 T4 模板的项目将启用最新的 C# 功能,例如内插字符串。 (我刚刚使用 Visual Studio 2017 15.6.2 和软件包的 2.7.0 版完成了此操作)。
这在 Visual Studio 2019 年对我不起作用,所以我求助于创建一个单独的项目,我用汇编指令引用了该项目。本质上,程序集是一个 class 功能块