简体   繁体   English

是否可以更改Visual Studio 2010运行时T4文本模板的输出CS文件的默认自动生成后缀?

[英]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. 我正在使用运行时T4文本模板。 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. 我希望预处理器生成的C#文件的输出扩展名为.tt.cs,但是,默认情况下,其扩展名为.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" #>

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. 首先创建您自己的单个文件生成器,该文件生成器从Microsoft.VisualStudio.TextTemplationg.VSHost.10.0.dll中的TemplatePreprocessor派生,并在Visual Studio中注册它。

Then overide ProcessTemplate() and after calling the base implementation, call SetFileExtension with whatever extension you want. 然后覆盖ProcessTemplate(),并在调用基本实现后,使用所需的任何扩展名调用SetFileExtension。

It's a lot of work though for such a small change. 尽管要进行这么小的更改,却需要做很多工作。

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. 可能对您很有用,因为他展示了如何使用预处理模板设置.cs,但前提是您要从另一个模板中调用模板。 Haven't found anything showing it's possible if run from in the C# or VB code. 没有发现任何表明可以从C#或VB代码中运行。

The <#@ output #> directive appears to have no effect on preprocessed templates. <#@ output#>指令似乎对预处理的模板没有影响。 No error is generated when a preprocessed template contains this directive. 预处理模板包含此指令时,不会产生任何错误。 The hosting application is responsible for changing the extension of the output file. 托管应用程序负责更改输出文件的扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM