When I right click my .tt files and select "run custom tool" from Visual Studio 2022 the .cs output files generate perfectly however when I try to run TextTransform.exe or TextTransformCore.exe from Powershell CLI the transformation fails with the following error:
error CS0103: Compiling transformation: The name 's_data' does not exist in the current context
But the name s_data is declared in a partial class that matched the template and is recognised fine when ran using the VS "run custom tool" option.
Code in SideNavTemplate.tt file:
<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<ul class="side_nav">
<# foreach (MenuItem sideItem in s_data) { #>
...
<# } #>
</ul>
Code in partial class:
using System.Xml.Linq;
namespace Excelerator.Models;
partial class SideNavTemplate
{
private List<MenuItem> s_data;
public SideNavTemplate(List<MenuItem> data) { s_data = data; }
}
public class MenuItem
{
public string label { get; set; }
public string route { get; set; }
public List<MenuItem> subMenu { get; set; }
public List<MenuItem> sideData { get; set; }
public FormItem form { get; set; }
public string role { get; set; }
}
public class FormItem
{
public string name { get; set; }
}
Tried calling TextTransform.exe from the directory both files are in but no joy it will not recognise the s_data property.
The custom tool property of the .tt file is TextTemplatingFilePreprocessor
Any help getting this to run would be greatly appreciated.
There is a possible solution here
Another way to go is to also include the partial classes in a .ttinclude file and generate those as well.
If you need T4 files to be generated before build events, you can consider using T4Executer