I'm trying to dynamically load an assembly into my T4 Generator. I've written a class which will locate the assembly in the packages folder:
public static class DynamicModuleLoader
{
public const string VERSION_GROUP_IDENTIFIER = "version";
public const string DEFAULT_PACKAGE_LOCATION = "lib\\net45";
public static void LoadLatestFromNugetPackages(string packageRootDir, string[] assemblyNames /*, bool usePreRelease = false*/)
{
foreach (var assemblyName in assemblyNames)
{
//var internalPattern = usePreRelease ? @"[\d]+[.\w-]*" : @"[\d.]+\\";
var internalPattern = @"[\d]+[.\w-]*";
//We need this regex to filter out other assemblies with similar names, as well as to parse versions
var regexMatchPattern = $@"(?i){assemblyName}\.(?<{VERSION_GROUP_IDENTIFIER}>({internalPattern}))";
var regex = new Regex(regexMatchPattern, RegexOptions.Compiled);
var subDirectories = Directory.GetDirectories(packageRootDir, $"{assemblyName}.*")
.Where(subDir => regex.IsMatch(subDir));
var targetDir = subDirectories
.OrderByDescending(subDir => regex.Match(subDir).Groups[VERSION_GROUP_IDENTIFIER].Value)
.FirstOrDefault();
var assemblyFullPath = $"{Path.Combine(targetDir, DEFAULT_PACKAGE_LOCATION)}/{assemblyName}.dll";
Assembly.Load(assemblyFullPath);
}
}
}
In my TT file I then attempt to load the dll dynamially.
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ import Namespace="System.Linq" #>
<#@ import Namespace="System.Collections.Generic" #>
<#@ import Namespace="System.IO" #>
<# DynamicModuleLoader.LoadLatestFromNugetPackages("$(SolutionDir)\\packages", new [] {"Services.Data" }); #>
<#@ import Namespace="Services.Data.Contracts" #>
<#@ import Namespace="Services.Data.Shared.Keys" #>
<#@ import Namespace="Services.Data.Enums" #>
I verified I am providing the correct path the to assembly yet I still receive an error:
Compiling transformation: The type or namespace name 'Enums' does not exist in the namespace 'Services.Data' (are you missing an assembly reference?) Apps.API.ServiceContracts C:\source\server\Apps.API.ServiceContracts\Data\Models.tt 27
Aucun commentaire:
Enregistrer un commentaire