Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 725
Crono Avatar asked Sep 10 '15 14:09

Crono


People also ask

What is the newest version of C?

C17. Published in June 2018 as ISO/IEC 9899:2018, C17 is the current standard for the C programming language.

What is the available feature of C?

The main features of C language include low-level access to memory, a simple set of keywords, and a clean style, these features make C language suitable for system programmings like an operating system or compiler development.

How is C used today?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

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.

like image 171
Panagiotis Kanavos Avatar answered Sep 20 '22 17:09

Panagiotis Kanavos


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.

like image 45
Douglas Avatar answered Sep 18 '22 17:09

Douglas