Can you use global variables inside a t4 template? -
how can use global variables in tt file?
if declare variable in header compile error if reference in function.
<#@ template debug="false" hostspecific="false" language="c#" #> <#@ output extension=".cs" #> <# int valueforthisfile = 35; somefunction(); #> <#+ void somefunction() { #> public void generatedcode() { int value = <#=valueforthisfile#>; } <#+ } #>
i know pass argument there hundreds of calls , syntactically tighter if avoid that. if 1 file hard code value there dozens of files have different settings , common include files generate code.
thanks,
john
i don't think possible. when t4 parses template generating class. <# #> contents injected single method on class while <#+ #> tags added methods class, allowing call them single method <# #> tags. scope of "valueforthisfile" variable limited single method. simple example, template:
<#@ template debug="false" hostspecific="false" language="c#" #> <#@ output extension=".cs" #> <# int valueforthisfile = 35; somefunction(); #> <#+ void somefunction() { return valueforthisfile; } #>
would generate class this:
class t4gen { private void mainwork() { int valueforthisfile = 35; this.somefunction(); } private void somefunction{ return valueforthisfile; } }
the variable "valueforthisfile" scoped mainwork function. actual class t4 generates more complicated see there no way have global variable in code that.
Comments
Post a Comment