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

自古美人都是妖i 提交于 2019-12-08 15:56:08

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/15843221/can-i-execute-code-from-another-file-in-a-t4-tt-template-for-code-generation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!