Ok, sono riuscito a ottenere il riferimento necessario all'assembly @FuleSnabel mi ha dato un suggerimento, anche se non ho usato il suo suggerimento.
Ecco una parte del mio template T4:
<#@ 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;
}
#>
Così, per trovare il percorso giusto per il montaggio ho dovuto ottenere riferimento al progetto in GetProject()
metodo e quindi utilizzare la proprietà del progetto OutputFileName
con prj.Properties.Item("OutputFileName").Value
. Dal momento che non sono riuscito a trovare da nessuna parte il progetto delle proprietà, ho usato l'enumerazione e un ciclo per ispezionare la raccolta Properties
e ho trovato ciò di cui avevo bisogno. Ecco un codice di ciclo:
<#
// ....
foreach(Property prop in prj.Properties)
{
#>
<#= prop.Name #>
<#
}
// ....
#>
Spero che questo vi aiuterà qualcuno.
Una possibilità è trovare il file .csproj, analizzarlo ed estrarre il percorso di output della build. È un file xml. – FuleSnabel
hm ... sì, probabilmente potrei, buon punto. Ci proverò. – Kosta