2

I've got a T4 template with a line like this:

Dim Variables = (From line in file.EnumerateLines() _
                Let data = line.Split("|".ToCharArray())
                Select new Variable With {.Name = data(0), .Type = data(1)}).ToList()

Variable is defined elsewhere in the file, and EnumerateLines is defined in a utility library referenced by the template. But I get tons of T4 compiler errors like Close_parens expected even though this is valid code that I cut and pasted from a VB code file in the IDE where it compiled just fine. I'm referencing all needed libraries and importing namespaces, but it seems like T4 will only accept VB code from 8 years ago. For example, this code is functionally identical, and compiles:

Dim Variables As New List(Of Variable)

for each lineX As String in EnumerateLines(file)
    Dim data = lineX.Split("|".ToCharArray)

    Dim v as New Variable
    v.Name = data(0)
    v.Type = data(1)

    Variables.Add(v)
next

How do I get T4 to understand LINQ, extension methods, initializers, and all the other stuff it doesn't seem to understand?

Update: Just to clarify, I have the needed assemblies and namespaces imported:

<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="Microsoft.VisualBasic" #>
Joshua Frank
  • 13,120
  • 11
  • 46
  • 95
  • 2
    This related question looks like it may guide you to the correct answer: http://stackoverflow.com/questions/247005/how-can-i-use-linq-in-a-t4-template – Anthony Nov 22 '13 at 20:27
  • For C# devs; in VS2012+ you get C#4 out of the box in T4 so the above is no longer needed. Sadly it seems T4+VB hasn't moved with the times. – Just another metaprogrammer Nov 27 '13 at 12:52

0 Answers0