Reputation: 2312
I'm trying to write some nice and structured t4 for code generation. I decompose my logic into reusable functions and put them into separate files (just like "normal" code). The problem is I cannot include a common file since it will be included multiple times.
For example, I have files 4 files: Core.tt
, File1.tt
, File2.tt
and MainTemplate.tt
.
Core.tt
is inclued in both File1.tt
and File2.tt
.
While 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 guess, the reason is that Core.tt
has been included twice. Is there a workaround for my problem? In c++ It's possible to add header guards: #ifndef xxx #define xxx #endif
Upvotes: 4
Views: 654
Reputation: 2781
I was having similar problem with the following error message:
Compiling transformation: The type 'GeneratedTextTransformation' already contains a definition for 'BaseCodegenTemplate'
I searched my project with 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 removed one of the occurrences from one of the files the error disappeared.
But because include statement for BaseCodegenTemplate.tt file now is missing in that file, some code in that file is highlighted with red color because type definitions contained in BaseCodegenTemplate.tt are missing. But the overall code compiles well: types defined in BaseCodegenTemplate.tt are included during the build because BaseCodegenTemplate.tt is referenced in another file.
Upvotes: 0
Reputation: 8095
Yes, this feature is built in to the T4 system.
The T4 'include' directive supports a 'once' attribute, which should ensure a template is only included once, preventing duplicates.
Example:
<#@ include file="filePath.tt" once="true" #>
Upvotes: 2
Reputation: 6606
Unfortunately, I've not been able to work out a way to get this to work, as there are hard limitations about where you can use #define in C#, and the #include comes in too late in the code.
Your best bet at this point is to log a feature request on http://visualstudio.uservoice.com and try to drum up some support.
Personally, I'd love to get this feature into the product.
Upvotes: 1