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

StackOverflow https://stackoverflow.com/questions/10941518

Question

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" #>
Was it helpful?

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.

OTHER TIPS

I found the following at this link. Might be of use to you as he shows how to set the .cs using a Preprocessed Template but only if you are calling the template from within another template. Haven't found anything showing it's possible if run from in the C# or VB code.

The <#@ output #> directive appears to have no effect on preprocessed templates. No error is generated when a preprocessed template contains this directive. The hosting application is responsible for changing the extension of the output file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top