All is good until you realize the following: You cannot recompile the library after a T4 template that uses it has been run in Visual Studio until you close that instance of VS. What a bummer - that destroys the tight edit->run template->edit cycle.
The solution: A directive processor that creates a temporary copy of the referenced assembly and loads that copy for the template generation. Each time the assembly changes, a new copy is created and the new copy is loaded for the next template generation (the templating AppDomain has to be force-unloaded to make this work). The copies are lazily cleaned up when they become unused.
I've added such a directive processor to the T4Toolbox project at codeplex. You can use it as follows:
<#@ VolatileAssembly processor="T4Toolbox.VolatileAssemblyProcessor" name="CodeGenerationLibrary.dll" #>
You should then add a reference to CodeGenerationLibrary from the project that contains the T4 template and set CopyLocal=true. If you cannot set a project reference, you can of course provide the relative path to the CodeGeneration.dll and ensure that it is built before code-gen happens.
If you don't want to get the entire T4Toolbox project, the code (with some names different from the T4Toolbox version) is pasted in the DSL Tools forum here.
4 comments: