Neil Trodden
Neil Trodden

Reputation: 4728

Error when attempting to pass a parameter to a 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 the template in visual studio, the property 'ClassName' is there in the properties window. That's what I want! When I enter text in there 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   

Hopefully, you can see what I want to do: I'd like my template to spit out a .cs file with a class named with the string I set in the property window in visual studio. Sadly, I am failing at a very early step!!

Upvotes: 4

Views: 3398

Answers (3)

Sri
Sri

Reputation: 1

I had the same issue when I was upgrading my project to VS2017 and I fixed it by creating templates in 2017 IDE.

Upvotes: 0

lavezhang
lavezhang

Reputation: 31

You shoud invoke Initialize() mehod first, like this:

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);

Upvotes: 3

Oleg Sych
Oleg Sych

Reputation: 227

Last time I tried, this scenario didn't work because there is no standard processor for the property directive and no support it in Visual Studio. You may be using the limited support provided for this directive by the Clarius T4 editor; I remember getting a similar error.

Upvotes: 0

Related Questions