문제

We have a .cs file and a .vb file that are each linked into dozens of projects in their respective languages to provide common assembly information. I'd like to set the common aspects between them in T4 by moving each to a template.

AssemblyInfo.cs and AssemblyInfo.vb should be the output file names. However, I know that normally, a T4 template for either of these would be named AssemblyInfo.tt.

Since I obviously can't have two T4 templates with the exact same name, how can I output two files that are different only by extension?

I can't rename the files to be written without un-linking them from many different projects and then having to re-link them again, so that's out of the question.

Is there some sort of directive that could be used to manually specify the output file name so that the templates could have different names?

도움이 되었습니까?

해결책

By default it is not possible to have multiple output files for a single T4 template. But out there are some implementations that offer you this possibility. For example, if you are using tangible's T4 Editor they provide a free template gallery containing a includable template named "TemplateFileManagerV2.1". Referencing this in your T4 template allows you to generate multiple output files, even in different projects of your solution.

Here's how that could work for you:

    <#@ template language="C#" hostSpecific="true" #>
    <#@ include file="TemplateFileManagerV2.1.ttinclude #>

    <#
      var manager = TemplateFileManager.Create(this);
      manager.StartNewFile("output.cs");
    #>
      // content here will be rendered into
      // output.cs file

    <#
      manager.StartNewFile("output.vb");
    #>

      // content here will be rendered into
      // output.vb file
    <#
      manager.Process();
    #>

You might have a tough time tough, since you're using two different output languages in the same T4 template. Thus the syntax highlighting will be messed up...

Hope that Helps

다른 팁

Just put more .ttN files in the same directory. And regulate the extension within each .ttN file:

AppResources.tt     <#@ output extension=".resx" #>
AppResources.tt2    <#@ output extension=".de.resx" #>
AppResources.tt3    <#@ output extension=".nl.resx" #>

It's much simpler than all that. Just append .tt to the desired file name and use:

<#@ output extension=""#> 

client_stubs.h.tt

<#@ output extension="" encoding="utf-8"#>
<#@ include file="Common.t4" #>

class client_stubs
{
<#
    foreach (string member in class_members.Keys)
    {
#>
    int <#=member#>;
<#
    }
#>
}

client_stubs.cpp.tt

<#@ output extension="" encoding="utf-8"#>
<#@ include file="Common.t4" #>
#include "client_stubs.h"
<#
    foreach (string member in class_members.Keys)
    {
#>
    int client_stubs::<#=member#> = <#=class_members[member]#>;
<#
    }
#>

Common.t4

<#@ template language="C#" #>
<#@ import namespace="System.Collections.Generic" #>
Dictionary<string, string> class_members = new Dictionary() {
    {"m_first", "1"},
    {"m_second", "2"}
};
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top