简体   繁体   中英

How can I use latest features of C# v6 in T4 templates?

I'm trying to run a new T4 template in Visual Studio 2015. However it fails to compile at this line:

var message = $"Linked table '{linkedTable}' does not exist.";

The compiler reports that the '$' character is unexpected. This syntax should however be valid in C# v6, according to the new string interpolation feature guidelines.

Is there a way to make the T4 templating engine use the newer C# version, other than to compile my code in an external library ?

UPDATE:

Here are the declaration elements for the file, as a reference:

<#@ 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" #>

Apart from the SqlModelDirective element this is pretty standard I think.

You can't use C# 6 in T4 templates right now as they don't use the latest compiler.

You can specify compiler options in the compilerOption attribute of the template directive. If the latest compiler was used, you could use:

<#@ template debug="false" hostspecific="false" language="C#" 
    compilerOptions="/langversion:6" #>

When I tried this I got the following error:

Compiling transformation: Invalid option '6' for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default

UPDATE

Found this discussion on the ASP.NET repository on Github. Seems the ASP.NET team is looking to replace T4 with Razor (which is not a bad idea BTW). Wonder if @davidfowl has more info ;)

UPDATE 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 , it works only on ASP.NET 4.x projects, not T4.

So no C# 6 in T4 for now.

You should upgrade to Visual Studio 2015 Update 2 , released on March 30, 2016, which introduces such functionality. Under its “Other Changes”:

Enhanced T4 text templates so that they now support C# 6.0.

However, the functionality is broken again in Visual Studio 2015 Update 3.

In Visual Studio 2017 (and probably 2015 too), adding the latest Microsoft.Net.Compilers nuget package to the project containing your T4 templates will enable the latest C# features such as interpolated strings. (I have just done this using Visual Studio 2017 15.6.2 and version 2.7.0 of the package).

This was not working for me in Visual Studio 2019 so I resorted to creating a separate project that I referenced with an assembly directive. Essentially the assembly is a class feature block

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM