如何在我们的 T4 模板生成器中包含自定义实用程序类? [英] How to include a custom utility class with our T4 templates generators?

查看:33
本文介绍了如何在我们的 T4 模板生成器中包含自定义实用程序类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我们的 T4 模板生成器中包含自定义实用程序类?我尝试向项目添加一个模块,但似乎没有办法将它包含在我的生成器中以使用它的方法.

How to include a custom utility class with our T4 templates generators ? I tried adding a module to the project but it seem there is no way to include it for my generators to use the methods from it.

谢谢.

推荐答案

您无法从模板访问项目中的任何代码.即使可以,它又将如何执行?模板在设计时执行.

You cannot access any code that is in your project, from your template. Even if you could, how would it execute? The template is executed at design time.

使用 Inherits 指令,给出您的实用程序类的名称.http://technet.microsoft.com/en-us/query/bb126474请参阅使用不同的实用方法集".你的类需要从 Microsoft.VisualStudio.TextTemplating.TextTransformation 继承

Use the Inherits directive, give the name of your utility class. http://technet.microsoft.com/en-us/query/bb126474 See 'Using a different set of utility methods'. Your class will need to inherit from Microsoft.VisualStudio.TextTemplating.TextTransformation

<#@ template inherits="MyUtilityClass" #>

那个,或者您可以将模块构建到程序集中,然后从模板中引用该程序集

That, or you can build your module into an assembly, then reference the assembly from within the template

<#@ assembly name="MyAssembly.Utilities" #>

或者您可以简单地将您的实用程序方法放在另一个模板中的类功能块中,然后将其包含在您的主模板中

or you can simply put your utility methods in a class feature block in another template then include it in your main template

<#@ include file="MyUtilities.tt" #>

这篇关于如何在我们的 T4 模板生成器中包含自定义实用程序类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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