nullreferenceexception - Executing T4 template: Host is null while hostspecific="True" -
i trying write console program takes file name of t4 template parameter, , processes template. because want users able update template without having recompile program.
the simplest solution found create second template within visual studio precompiles c# class, in turn executes external template "template.tt":
<#@ template language="c#" hostspecific="true" #> <#@ import namespace="system.io" #> <#@ import namespace="microsoft.visualstudio.texttemplating" #> <# string template = file.readalltext(host.resolvepath("template.tt")); engine engine = new engine(); string output = engine.processtemplate(template, host); write(output); #>
within program.cs of console program, execute "calling" template:
string templatetext = new caller().transformtext();
problem when run (f5), throws nullreferenceexception on first line in caller template, because host null.
however, thought setting hostspecific true give me host.
what need host? don't want roll own. alternatively, there better way execute external t4 template?
according msdn documentation hostspecific attribute provides access host-property inside t4 template. not guarantee presence of host. design-time templates (that transformed in visual studio) visual studio , it's t4 engine provides host. unfortunately microsoft.visualstudio.texttemplating.dll not redistributable assembly. won't have host in run-time scenario.
as stated, need write complete t4 engine , host yourself...
if users need write/alter t4 templates, have knowledge programming , writing text templating. think of way make them use visual studio in way?
alternatively (if changes user make small) try use precompiled template works on other input (xml-file, ini-file, command-line-parameters, ...).
sorry not having better news...
Comments
Post a Comment