views:

92

answers:

1

I'm working on my first T4 code generation tool to add some Stored Procedure helper code to my project. I've created custom types (e.g. StoredProcedure and StoredProcedureParameter to help with my code generation and have included the assembly and namespace references in my code:

<#@ template debug="false" hostspecific="false" language="VB" #>
<#@ output extension=".generated.vb" #>
<#@ assembly name="$(TargetPath)" #>
<#@ import namespace="StoredProcCodeGenerator" #>

This allows me to use my custom types in my T4 template code. However, because my custom types exist in the same project as the T4 template code, I can't recompile my project once I run the template code without restarting Visual Studio. This isn't very much fun.

I read a great article that addresses this exact issue by using the T4 Toolbox, but it's not working. Either I'm implementing the VolatileAssembly directive wrong or the T4 toolbox simply didn't get installed. I'm not sure that the toolbox got installed correctly (I'm using VS 2010 on Win XP).

What are some ways that I might be able to fix this problem?

+1  A: 

You need to remove the previous assembly reference and then add the VolatileAssembly reference. If you don't remove the regular assembly reference first, you'll get an error that it has already been added when you add the VolatileAssembly reference.

<#@ template debug="false" hostspecific="false" language="VB" #>
<#@ output extension=".generated.vb" #>

<#@ assembly name="$(TargetPath)" #>

<#@ VolatileAssembly processor="T4Toolbox.VolatileAssemblyProcessor" 
    name="$(TargetPath)" #>
<#@ import namespace="StoredProcCodeGenerator" #>  

Now you can continue to build your project and use types defined in that project within your T4 templates.

Ben McCormack
What happens when you have neither? (I have neither "assembly" nor "VolatileAssembly" and classes from the project that contains the .tt file are always accessible for me.)
Kirk Woll
@Kirk That's interesting. If I don't include an assembly reference, I'll get an error that it can't find my custom types. For example, I have a `StoreProcedure` type that I consume in my .tt file. If I don't reference my assembly, it won't be able to find the `StoredProcedure` type.
Ben McCormack