T4 VS2010 template gets host assembly

I want to get a link to the project assembly in which the T4 template is located. I know that I can get the project path, for example, Host.ResolveAssemblyReference("$(ProjectDir)") , and I could add bin\\debug\\{projectName}.dll , because my assembly names are called the project name, but this is not always the case, and I am creating a reusable template, so I need a path to a dll, or most preferably an Assembly instance. I also found how to get a link to Project , as described here in the GetProjectContainingT4File method, but then what?

Is there any way to get it?

By the way, I need this link to access certain types and generate some code from them.

+6
source share
2 answers

Well, I managed to get the necessary link to the @FuleSnabel assembly, gave me a hint, although I did not use his suggestion.

Here is 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; } #> 

So, in order to find the correct path to the assembly, I had to get the project link in the GetProject() method, and then use the project property OutputFileName with prj.Properties.Item("OutputFileName").Value . Since I could not find anywhere that the properties project had, I used an enumeration and a loop to check the Properties collection, and then found what I needed. Here is the loop code:

 <# // .... foreach(Property prop in prj.Properties) { #> <#= prop.Name #> <# } // .... #> 

Hope this helps someone.

+9
source

The following simple code worked for me (VS 2013):

 var path = this.Host.ResolveAssemblyReference("$(TargetPath)"); var asm = Assembly.LoadFrom(path); 

You can also find the $(...) properties in the psot project editor.

+9
source

All Articles