5

How can i use generated file's name in template? I'd like to something like this:

// This file: <#= OutputFileName #> was autogenerated

How can i do that in T4 or in T4Toolbox?

Simon
  • 2,329
  • 4
  • 30
  • 49

2 Answers2

7

This seemed like something that should be as simple as reading a property but after reflecting over the Host variable and disassembling the text generator code I think the simplest way of doing this is:

<#@ template language="C#" hostspecific="true"  #>
<#@ import namespace="System.IO"                #>

// <#=Path.ChangeExtension (Host.TemplateFile, "cs")#>
  • It's not exactly what i meant. I don't want to specify extension manually. I want everything to be automatic. Event extension should be taken from template's variable. When i'm setting <#@ output extension="cs"#> where is it? Is there any access to this field – Simon Aug 11 '11 at 07:30
  • 1
    <#@ output extension="cs"#> calls Host.SetFileExtension ("cs"). As far as I can tell there's no public methods/properties to get the FileExtension. I tried using reflection to retrieve it (just to try) but it's complicated by the fact that it seems the Host runs in another AppDomain. – Just another metaprogrammer Aug 11 '11 at 08:52
2

It better to define it as a directive

for example for a xaml file

<#@ output extension="xaml" #>

or for txt

<#@ output extension=".txt" #>
404Dreamer_ML
  • 890
  • 3
  • 14
  • 25