在 T4 代码生成中,如何从引用的程序集中获取类型? [英] In T4 code generation, how can I grab a type from a referenced assembly?
问题描述
由于 T4 在项目上下文之外运行,我无权访问当前程序集或其他程序集.如何注册对引用程序集的访问权限,然后从中获取类型?
Since T4 runs outside of the context of a project, I don't have access to the current assembly or other assemblies. How can I register access to a referenced assembly, then grab a type from it?
推荐答案
我猜您想访问您项目中建筑物的程序集.我在下面的示例代码中所做的是将一个名为 TestLib 的项目添加到我的解决方案中,我将其构建到解决方案目录中的文件夹 .\T4Libs 中.TestLib 有一个名为 MyHelper 的类,它包含一个幻数.
I am guessing you want to access an assembly your building in your project. What I've done in the sample code below is adding a project to my solution called TestLib which I build to the folder .\T4Libs in the solution directory. TestLib has a class called MyHelper which holds a magic number.
FYI VS2010 SP1 添加了一个很棒的功能,它不再锁定导入的程序集,因此这是一个很好的版本.
FYI VS2010 SP1 added the great feature in that it no longer locks the imported assemblies so that's a good version to use.
<#@ assembly name = "$(SolutionDir)\T4Libs\TestLib.dll" #>
<#@ import namespace = "TestLib" #>
<#
var myHelper = new MyHelper ();
#>
// <#=myHelper.MagicNumber#>
这篇关于在 T4 代码生成中,如何从引用的程序集中获取类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!