T4 "header protection" for included files

I am trying to write a nice and structured t4 to generate code. I decompose my logic into reusable functions and put them in separate files (like "regular" code). The problem is that I cannot include the shared file, as it will be included several times.

For example, I have 4 files files: Core.tt , File1.tt , File2.tt and MainTemplate.tt . Core.tt included in both File1.tt and File2.tt . So far, File1.tt and File2.tt are included in MainTemplate.tt

When I want to generate output from MainTemplate.tt , I get the following error:

 Error 8 Compiling transformation: The type 'Microsoft.VisualStudio.TextTemplating62CD98C8FF0EB737CAFBD5ED17A158C3.GeneratedTextTransformation' already contains a definition for 'PropertyAttribute' 

I think the reason is that Core.tt been enabled twice. Is there a workaround for my problem? In C ++, you can add header protectors: #ifndef xxx #define xxx #endif

+4
source share
3 answers

Unfortunately, I have not been able to find a way to make this work, since there are strict restrictions on where you can use #define in C #, and #include is too late in the code.

It is best at this stage to register a feature request at http://visualstudio.uservoice.com and try to raise some support.

Personally, I would like to get this feature in the product.

+1
source

Yes, this feature is built into the T4 system.

The T4 'include' directive supports the "once" attribute , which should ensure that the template is included only once, preventing duplicates.

Example:

 <#@ include file="filePath.tt" once="true" #> 
+2
source

I had a similar problem with the following error message:

 Compiling transformation: The type 'GeneratedTextTransformation' already contains a definition for 'BaseCodegenTemplate' 

I searched my project using CTRL + SHIFT + F for this line of text:

 <#@ include file="BaseCodegenTemplate.tt" #> 

and found 2 occurrences of this line in 2 different files. After I deleted one of the entries from one of the files, the error disappeared.

But since there is no include specification for the BaseCodegenTemplate.tt file in this file, some code in this file is highlighted in red because there are no type definitions contained in BaseCodegenTemplate.tt. But the general code compiles well: the types defined in BaseCodegenTemplate.tt are included at build time because BaseCodegenTemplate.tt is referenced in another file.

0
source

All Articles