Can I execute code from another file in the T4.tt template to generate the code?

I use several T4 templates to generate multiple T4 files, and most of them use a lot of the same code, so naturally I would like to make a function out of it and put it in another file that all T4 files can access.

However, I cannot find a way or Google to use the code stored in another file.

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

+8
c # code-generation t4
source share
1 answer

From your question, it seems you are just using T4 in a regular template file in your solution. In this case, you can simply use the <#@ include #> directive to pull out your shared code. This is a mechanism for including text, akin to C / C ++ #include , so you can move as much or less as you want to share with other files.

See the docs here .

It is worth noting that currently the include directive does not work in ASP.Net view templates.

If you want to share the code with your regular C # project, this is possible, but you need to assemble the common code into an assembly that you can reference. You cannot just use the <#@ include #> directive to pull the .cs file directly, because directives are not embedded inside control blocks or classes.

You can reference an auxiliary assembly containing your shared code using the <#@ assembly #> directive registered here .

+7
source share

All Articles