我可以从代码生成一个T4模板.TT另一个文件执行代码? [英] Can I execute code from another file in a T4 .tt template for code generation?

查看:539
本文介绍了我可以从代码生成一个T4模板.TT另一个文件执行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用几个T4模板生成多个文件,T4,且大多有着很多相同的代码,所以很自然我想作一个功能了出来,并把它放在不同的文件,所有的T4的文件可以访问。

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?

推荐答案

从你的问题,这听起来像你只是用在解决方案中的一个正常的模板文件T4。
在这种情况下,你可以使用<#@#包括GT&; 指令在你的共享代码拉。这是一个原始文本包含机制,类似于C / C ++的的#include ,这样可以为您愿意分享到其他文件移动尽可能多或尽可能少。

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.

请参阅文档的此处

值得注意的是,目前,include指令在ASP.Net视图模板脚手架不起作用。

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

如果你想与你的正常的C#项目共享代码,这是可能的,但你需要建立共享代码到你可以参考的组件。这是不可能只使用<#@包括#> 指令在的.cs 文件中直接拉。作为指令做内部控制或类功能块不能嵌套

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.

您可以参考使用包含您共享代码的辅助组件<# @#组装GT&; 指令记录的这里

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

这篇关于我可以从代码生成一个T4模板.TT另一个文件执行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆