您无法从模板访问项目中的任何代码.即使你可以,它会如何执行?模板在设计时执行.
使用Inherits指令,给出实用程序类的名称.http://technet.microsoft.com/en-us/query/bb126474请参阅"使用一组不同的实用程序方法".您的类需要继承自Microsoft.VisualStudio.TextTemplating.TextTransformation
<#@ template inherits="MyUtilityClass" #>
Run Code Online (Sandbox Code Playgroud)
那,或者您可以将模块构建到程序集中,然后从模板中引用程序集
<#@ assembly name="MyAssembly.Utilities" #>
Run Code Online (Sandbox Code Playgroud)
或者您可以简单地将实用程序方法放在另一个模板的类功能块中,然后将其包含在主模板中
<#@ include file="MyUtilities.tt" #>
Run Code Online (Sandbox Code Playgroud)