Search code examples
c#visual-studio-2010t4texttemplate

Can I change the default auto-generated suffix a Visual Studio 2010 run-time T4 text template's outputted cs file?


I am using a run-time T4 Text Template. I wish for the preprocessor generated C# file to have an output extension of .tt.cs, however, by default, it only has an extension of .cs. If this were a design-time text template, I could add an output extension directive to enforce this behaviour. However, it appears that such directives don't have any affect on run-time templates. Does anyone know of a way to accomplish this with run-time templates?

<#@ output extension=".tt.cs" #>

Solution

  • There's no trivial way, but it can be done.

    First create your own single file generator, derived from TemplatePreprocessor in Microsoft.VisualStudio.TextTemplationg.VSHost.10.0.dll and register it with Visual Studio.

    Then overide ProcessTemplate() and after calling the base implementation, call SetFileExtension with whatever extension you want.

    It's a lot of work though for such a small change.