Error while trying to pass parameter to t4 template

I'm trying (and failing) to write a simple template file:

<#@ template language="C#" hostspecific="True" debug="True" #>
<#@ output extension="cs" #>
<#@ include file="T4Toolbox.tt" #>
<#@ property name="ClassName" processor="PropertyProcessor" type="System.String" #>

public class <#= ClassName #>
{
}

      

When I click on a template in visual studio, the "ClassName" property is in the properties window. This is what I want! When I enter text and build, I get the following error:

 Error  1   Running transformation: System.ArgumentNullException: Value cannot be null.
Parameter name: objectToConvert
   at Microsoft.VisualStudio.TextTemplating.ToStringHelper.ToStringWithCulture(Object objectToConvert)
   at Microsoft.VisualStudio.TextTemplating32ED7F6BD49D2C3984C2CB7194792D4B.GeneratedTextTransformation.TransformText() in c:\Users\neilt.PAV12\Documents\Visual Studio 2008\Projects\ConsoleApplication2\ConsoleApplication2\ClassMaker.tt:line 6  C:\Users\neilt.PAV12\Documents\Visual Studio 2008\Projects\ConsoleApplication2\ConsoleApplication2\ClassMaker.tt    1   1   

      

Hope you can see what I want to do: I would like my template to spit out a .cs file with a class named as specified in the properties window in visual studio. Unfortunately, I fail very early on!

+3


a source to share


4 answers


The last time I tried it , this scenario did not work because there is no standard processor for the property directive and does not support it in Visual Studio. You can use the limited support provided by this directive by the Clarius T4 editor; I remember getting a similar error.



0


a source


First you call the Initialize()

mehod, for example:



ClassTemplate t = new ClassTemplate();
t.Session = new Dictionary<string, object>();
t.Session["ClassName"] = "Person";

t.Initialize();//This is important.

string output = t.TransformText();
Console.WriteLine(output);

      

+2


a source


0


a source


I had the same problem when I updated my project to VS2017 and I fixed it by creating templates in the 2017 IDE.

0


a source







All Articles