Loading a NuGet assembly into a T4 template

I need to reference Type in one of the assemblies referenced by the project containing my Visual Studio T4 template. However, the reference assembly is installed from the NuGet package. As this Nuget link evolves, there will be a path that NuGet puts in the folder of my solution packages. For example, suppose my NuGet package is:

  • Facade.Contract.1.0.1 alpha

Then the relative path to it from my project:

  • .. \ packages \ Facade.Contract.1.0.1-alpha \ Lib \ NET4 \ Facade.Contract.dll

If the pre-publication is updated to beta, this path will change. When the package is released, the path will change. And every time the path changes, the pipeline in my * .tt file is out of date:

  • <# @assembly name = ".. \ packages \ Facade.Contract.1.0.1-alpha \ lib \ net4 \ Facade.Contract.dll" #>

I don't think there is a way to do this directly with the build directive; however, I am open to some crazy ideas. Can I load the assembly myself into the current or subordinate or just to reflect the AppDomain?

I think I could, but I'm not sure how to dynamically detect the link assembly path in the project links using T4 logic.

Any ideas?

+5
source share
2 answers

I found a solution using VSLangProject, as suggested in this article: http://t4-editor.tangible-engineering.com/blog/add-references-to-visual-studio-project-from-t4-template.html

For string stringContractReferenceAssembly a, to identify the name of the reference assembly in my containing project and serviceContractReferenceType to identify the type inside this assembly, the following worked:

var templateItem = dte.Solution.FindProjectItem(this.Host.TemplateFile); var project = templateItem.ContainingProject; var vsProject = project.Object as VSLangProj.VSProject; foreach(var referenceObj in vsProject.References) { var reference = (VSLangProj.Reference)referenceObj; if(reference.Name != serviceContractReferenceAssembly) continue; var serviceContractAssembly = Assembly.LoadFile(reference.Path); var serviceContractType = serviceContractAssembly.GetType(serviceContractReferenceType); // Do something with it here } 
+3
source

The Nuget team has made available an extension that allows you to control packages in a solution / project. Therefore, if you have control over the environment and you can make sure that everyone has this installed, you can search for installed packages and then dynamically download them at runtime T4. Since these Nuget assemblies have already been completed and are not part of your solution / project, I would have thought that using standard Assembly.Load would work, but you would need to check this out.

+2
source

Source: https://habr.com/ru/post/1205474/


All Articles