How to use T4 and Visual Studio to extend a partial class

I wanted to use T4 to create properties for a partial class. I am facing a configuration issue where my .TT file is in the same project as the class file that I want to extend. Therefore, if I need to include an assembly in a .TT file, it will be blocked. I tried command line in pre-build but the problem is VS always wants to recompile the .TT file with the project.

The only solution I can think of is renaming the .tt files to say .t4 and then using the 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?

+1


source to share


1 answer


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



+3


source







All Articles