This project is read-only.

Pre-compiled Templates - issues?

Mar 9, 2011 at 8:30 AM

Hello all,

I've been using t4 templates for a short time. Due to lack or awkward features I decided to give t4 Toolbox a try. First fo all: it worked perfectly well using the samples/instructions given by Oleg's posts regarding Templates/Generators!

However, when I switched to pre-compiled Templates, some things seemed to be not as smooth as before.

I made it work now, but want to share my experiences having not found any of this information on the discussion board (this may be just my search result, though ;-) Maybe Oleg has some comments about this as well, since I might have done something wrong.

So, here's what I tried to achieve:

1) create a pre-compiled t4 template to be automatically compiled (to a .cs) at VS build-time within a project (.dll)

2) use this pre-compiled template (.cs) from a t4 script within another project (.dll), generating some accessor class (.cs).

[I think this is a common target, nothing special at all.]

Here's what I did to achive goal 1) [goal 2) worked straight forward as expected]:

a) Create a pre-compiled template "" using Add->New Item in VS with type (Code Generation -> Preprocessed Template).

=> and MyTemplate.partial.cs were created.

b) Did not change any properties of both files. (?)

c) Reference toolbox.10.0 in my project.

d) Added generation logic to and added "public override string TransformText() { ...}" to MyTemplate.partial.cs

=> Did not compile since "public override string TransformText()" has been added already to auto-generated MyTemplate.cs by TextTemplatingFilePreprocessor.

e) Did add "master class" MyRealTemplate : MyTemplate" to MyTemplate.partial.cs and moved my TranformText()-method there. [Problem d) was solved.]

=> Did not compile since "ToStringInstanceHelper" (as used in auto-generated MyTemplate.cs) was unknown.

f) Added code to class MyTemplate (in MyTemplate.partial.cs) that has formerly been generated by VS, but is not anymore, basically Property ToStringInstanceHelper


=> Did compile.

Now, don't get me wrong - it works and all - but I wonder, whether I did something wrong or whether this is a VS 2010 issue not yet talked about or ...

Any feedback is welcome!



Apr 20, 2011 at 9:09 PM

This issue is a result of a change to introduced in SP1 of VS 2010:

So far, I have not found an elegant solution to the problem, however you can find some hacks that will be removed every time you regen code.

May 3, 2011 at 3:51 PM

The missing ToStringHelper will be added to the Template class in the next release of the T4 Toolbox. Until then, you can use statement blocks instead of expression blocks. So instead of

<#= myExpression #>


<# this.Write(myExpression.ToString()); #>

With the pre-processed templates in T4 Toolbox, you shouldn't override the TransformText method yourself, this method is generated by the preprocessor. If you want to have your own custom base class, instead of the T4Toolbox.Template, specify it using the <#@ template #> directive in the preprocessed .tt file.

May 4, 2011 at 5:46 AM

Thank you for your reponses clearifying this issue!