如何在T4模板中使用C#v6的最新功能?

栏目: ASP.NET · 发布时间: 7年前

内容简介:http://stackoverflow.com/questions/32505209/how-can-i-use-latest-features-of-c-sharp-v6-in-t4-templates

我试图在Visual Studio 2015中运行一个新的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

UPDATE

在Github上的ASP.NET存储库中找到 this discussion .看起来ASP.NET团队正在寻找用剃刀替代T4(这不是一个坏主意BTW).想知道@davidfowl是否有更多的信息;)

更新2

David Fowler responded on Twitter – T4使用尚未更新使用Roslyn的CodeDom.虽然有一个带有 replacement CodeDom providers 的NuGet软件包,但它只适用于ASP.NET 4.x项目,而不是T4.

所以现在T4中没有C#6.

http://stackoverflow.com/questions/32505209/how-can-i-use-latest-features-of-c-sharp-v6-in-t4-templates


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Linux Device Drivers

Linux Device Drivers

Jonathan Corbet、Alessandro Rubini、Greg Kroah-Hartman / O'Reilly Media / 2005-2-17 / USD 39.95

Device drivers literally drive everything you're interested in--disks, monitors, keyboards, modems--everything outside the computer chip and memory. And writing device drivers is one of the few areas ......一起来看看 《Linux Device Drivers》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具