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.
C17. Published in June 2018 as ISO/IEC 9899:2018, C17 is the current standard for the C programming language.
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.
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 ...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With