T4模板VS2010获取主机组件 [英] T4 template VS2010 get host assembly
问题描述
我想获得一个T4模板所在项目的组装参考.我知道我可以使用例如Host.ResolveAssemblyReference("$(ProjectDir)")
来获取项目的路径,并且我可以添加bin\\debug\\{projectName}.dll
,因为我的程序集名称是由项目名称命名的,但并非总是如此,并且我正在创建可重用的模板,因此我需要dll或最优选Assembly
实例的路径.
我还发现了如何获得对Project
的引用,如此处在方法GetProjectContainingT4File
中,但是那又是什么?
I want to get a reference to assembly of a project in which T4 template is. I know I can get path to the project with, for example Host.ResolveAssemblyReference("$(ProjectDir)")
and I could maybe add bin\\debug\\{projectName}.dll
because my assembly names are named by project name, but that is not always the case and I'm creating reusable template, so I need path to dll or most preferably Assembly
instance.
I have also found how to get reference to the Project
as explained here in method GetProjectContainingT4File
, but then what?
有没有办法得到它?
顺便说一句,我需要该引用来访问特定类型并从中生成一些代码.
BTW, I need that reference to access specific types and generate some code from them.
推荐答案
好吧,尽管我没有使用他的建议,但我设法获得了所需的程序集引用@FuleSnabel给了我一个提示.
Ok, I managed to get the needed reference to assembly @FuleSnabel gave me a hint, although I did't use his suggestion.
这是我的T4模板的一部分:
Here's a part of my T4 template:
<#@ template debug="true" hostSpecific="true" #>
<#@ output extension=".output" #>
<#@ Assembly Name="System.Core.dll" #>
<#@ Assembly Name="System.Windows.Forms.dll" #>
<#@ Assembly Name="System.Xml.Linq.dll" #>
<#@ Assembly Name="Microsoft.VisualStudio.Shell.Interop.8.0" #>
<#@ Assembly Name="EnvDTE" #>
<#@ Assembly Name="EnvDTE80" #>
<#@ Assembly Name="VSLangProj" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Xml.Linq" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="EnvDTE80" #>
<#@ include file="T4Toolbox.tt" #>
<#
Project prj = GetProject();
string fileName = "$(ProjectDir)bin\\debug\\" + prj.Properties.Item("OutputFileName").Value;
string path = Host.ResolveAssemblyReference(fileName);
Assembly asm = Assembly.LoadFrom(path);
// ....
#>
// generated code goes here
<#+
Project GetProject()
{
var serviceProvider = Host as IServiceProvider;
if (serviceProvider == null)
{
throw new Exception("Visual Studio host not found!");
}
DTE dte = serviceProvider.GetService(typeof(SDTE)) as DTE;
if (dte == null)
{
throw new Exception("Visual Studio host not found!");
}
ProjectItem projectItem = dte.Solution.FindProjectItem(Host.TemplateFile);
if (projectItem.Document == null) {
projectItem.Open(Constants.vsViewKindCode);
}
return projectItem.ContainingProject;
}
#>
因此,要找到正确的组装路径,我必须在GetProject()
方法中获得对项目的引用,然后将项目的属性OutputFileName
与prj.Properties.Item("OutputFileName").Value
一起使用.由于我在任何地方都找不到属性项目所具有的内容,因此我使用了枚举和循环来检查Properties
集合,然后找到了我需要的东西.这是一个循环代码:
So, to find right path to assembly I had to get reference to the project in GetProject()
method and then use project's property OutputFileName
with prj.Properties.Item("OutputFileName").Value
. Since I couldn't find anywhere what properties project has, I used enumeration and a loop to inspect Properties
collection and then found what I need. Here's a loop code:
<#
// ....
foreach(Property prop in prj.Properties)
{
#>
<#= prop.Name #>
<#
}
// ....
#>
我希望这会对某人有所帮助.
I hope this will help someone.
这篇关于T4模板VS2010获取主机组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!