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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With