In T4 code generation, how can I grab a type from a referenced assembly?

☆樱花仙子☆ 提交于 2019-12-22 08:06:08

问题


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?


回答1:


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 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#>



回答2:


Are you looking for the Assembly directive and/or are you looking for something in the System.Reflection.Assembly class (like LoadFile)?



来源:https://stackoverflow.com/questions/9318017/in-t4-code-generation-how-can-i-grab-a-type-from-a-referenced-assembly

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