T4 output filename
Asked Answered
A

2

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?

Allegiance answered 10/8, 2011 at 17:47 Comment(0)
H
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")#>
Headache answered 10/8, 2011 at 19:31 Comment(2)
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 fieldAllegiance
<#@ 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.Headache
D
2

It better to define it as a directive

for example for a xaml file

<#@ output extension="xaml" #>

or for txt

<#@ output extension=".txt" #>
Denominator answered 12/8, 2011 at 12:6 Comment(1)
OP was asking about how to get the output file name, so your response is OT.Actress

© 2022 - 2024 — McMap. All rights reserved.