tags:

views:

503

answers:

1

I wanted to use T4 to generate properties for a partial class. I'm running into a configuration problem where my .TT file is in the same project as the class file I want to extend. So if I have to include the assembly in the .TT file it get's locked. I tried to do a command line in the pre-build but the problem there is that VS always wants to recompile the .TT file with the project.

The only solution I can think of is to rename the .tt files to say .t4 and then use a pre-build command with TextTransform -out to create the .cs file in the project directory.

Can anyone think of a cleaner way to do this?

+2  A: 

Assuming that locking is caused by your template using Reflection to read metadata of the partial class you need to extend, you could solve the locking problem if you use CodeModel. This API is provided by Visual Studio and allows you to get the metadata directly from the source file, without the need to compile the partial class or load the compiled DLL. Here is an example of a T4 code generator that uses this approach: http://www.olegsych.com/2008/07/t4-template-for-generating-sql-view-from-csharp-enumeration

Oleg Sych
Thanks Oleg. I'll give this a try right away.
GoClimbColorado