我怎么能得到真正的路径从宏路径像$(TargetDir)

本文关键字:路径 TargetDir 怎么能 | 更新日期: 2023-09-27 18:02:38

在t4文本模板中,我试图使用:Assembly.LoadFile(dllPath) .

加载dll。

但是它返回给我错误:需要绝对路径信息

dll路径为:var dllPath = "$(TargetDir)Project.dll"$(SolutionDir)Project''bin''debug''Project.dll

我如何转换"$(TargetDir)Project.dll"在他真正的形式"C:'Users'....'Project.dll"之前发送到Assembly.LoadFile

我怎么能得到真正的路径从宏路径像$(TargetDir)

在T4中,我使用ttinclude来禁用重写结果,只允许它在使用名为"RegenCodeFiles"的特定命名解决方案配置时继续生成代码。

这里有几种不同的方法来解析路径,您可能会发现它们很有用,还有一个程序集解析器,它可以遍历packages文件夹(它是硬编码的,应该查找nuget.config)。

我要警告的一件事(仅仅因为它是一个麻烦的工作流)是试图引用TT是其中一部分的TT中的程序集。

include是这样被调用的…

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ include file="CodeGenHelpers.ttinclude" #>
var regenHelper = new CodeGenHelpers(Host);
regenHelper.SetupBinPathAssemblyResolution();
if (regenHelper.ShouldRegenerate() == false)
{
    Debug.WriteLine($"Not Regenerating File");
    var existingFileName = Path.ChangeExtension(Host.TemplateFile, "cs"); 
    var fileContent = File.ReadAllText(existingFileName);
    return fileContent;
}
Debug.WriteLine($"Regenerating File");
// ...rest of T4

CodeGenHelpers.ttinclude

<#@ assembly name="Microsoft.VisualStudio.Shell.Interop.8.0" #>
<#@ assembly Name="EnvDTE" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#+
    public class CodeGenHelpers
    {
        private static string _ConfigName;
        private static bool? _ShouldRegenerate;
        private ITextTemplatingEngineHost _Host;
        private static DTE _Dte;
        public CodeGenHelpers(ITextTemplatingEngineHost host)
        {
            _Host = host;
        }
        public  bool ShouldRegenerate()
        {
            if (_ShouldRegenerate.HasValue == false)
            {
                var configName = GetConfigName();   
                _ShouldRegenerate = "RegenCodeFiles".Equals(configName, StringComparison.OrdinalIgnoreCase);
            }
            return _ShouldRegenerate.Value;
        }
        public string GetConfigName()
        {
            if (string.IsNullOrWhiteSpace(_ConfigName))
            {
                _ConfigName = GetDte().Solution.SolutionBuild.ActiveConfiguration.Name;
            }
            return _ConfigName;
        }
        public DTE GetDte()
        {
            if (_Dte == null)
            {
                var serviceProvider = _Host as IServiceProvider;
                _Dte = serviceProvider.GetService(typeof(SDTE)) as DTE;
            }
            return _Dte;
        }
        public void SetupBinPathAssemblyResolution()
        {
            AppDomain.CurrentDomain.AssemblyResolve += ResolveMeh;
        }
        //load from nuget packages first
        public Assembly ResolveMeh(object sender, ResolveEventArgs args)
        {
            var currentFolder = new FileInfo(_Host.ResolvePath(_Host.TemplateFile)).DirectoryName;
            var configName = GetConfigName();
            //probably look at the project properties in DTE, but whatever
            var d = _Dte;
            var assemblyName = new AssemblyName(args.Name).Name + ".dll";
            var assemblyLoadFolder = Path.Combine(currentFolder, $"bin''{configName}");
            var assemblyPath = Path.Combine(assemblyLoadFolder, assemblyName);
            if (System.IO.File.Exists(assemblyPath) == true) 
            {
                var assembly = Assembly.LoadFrom(assemblyPath);
                return assembly;
            }
            //there are nuget services for vs, but they are installed via nuget package, 
            // which is what this is looking for so I dont know if it will work. 
            // https://www.nuget.org/packages/NuGet.VisualStudio

            var solutionFilePath = GetDte().FullName;
            var solutionFile = GetDte().FileName;
            var solutionFolder = solutionFilePath.Replace(solutionFile, string.Empty);
            var packagesFolder = System.IO.Path.Combine(solutionFolder, "packages");
            var files = System.IO.Directory.GetFiles(packagesFolder, assemblyName, SearchOption.AllDirectories);
            if (files.Length > 0)
            {
                var assembly = Assembly.LoadFrom(files[0]); //prob also check target fw
                return assembly;
            }
            return null;
        }
    }
#>