Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I execute code from another file in a T4 .tt template for code generation?

I'm using several T4 templates to generate several T4 files, and most of them share a lot of the same code, so naturally I would like to make a function out of it and put it in a different file that all of the T4 files could access.

However I can't seem to find a way, or google for a way to actually use code stored in another file.

Is this possible? and if so, is there a simple example of how to do so?

like image 373
Kyle Avatar asked Apr 05 '13 20:04

Kyle


1 Answers

From your question, it sounds like you're just using T4 in a normal template file in your solution. In this case, you can just use the <#@ include #> directive to pull in your shared code. It is a raw text inclusion mechanism, akin to C/C++'s #include, so you can move as much or as little as you care to share into other files.

See docs here.

It's worth noting that, at present, the include directive does not work in ASP.Net view scaffolding templates.

If you want to share code with your regular C# project, that is possible, but you need to build the shared code into an assembly you can reference. It's not possible to just use the <#@ include #> directive to pull in a .cs file directly as the directives don't nest inside control or class feature blocks.

You can reference a helper assembly containing your shared code using the <#@ assembly #> directive documented here.

like image 74
GarethJ Avatar answered Sep 19 '22 06:09

GarethJ