【问题标题】:Visual Studio extension: Change the hint path of an assembly referenceVisual Studio 扩展:更改程序集引用的提示路径
【发布时间】:2017-04-01 20:06:33
【问题描述】:

我正在编写 Visual Studio 扩展,我想更改 C# 项目的程序集引用的提示路径,而不触发“检测到文件修改”对话框。

<Reference Include="SomeAssembly">
  <HintPath>C:\ChangeMe\SomeAssembly.dll</HintPath>
</Reference>

但是在 VSLangProj110.Reference5-interface 中我找不到任何可以使用的属性。 (通过VSLangProj140.VSProject3.References访问)

【问题讨论】:

  • 为什么?标准程序集位于“C:\Program Files (x86)\Reference Assemblies\...”下并由 Visual Studio 管理,您自己的源代码应通过项目引用链接,其他二进制文件最好作为 Nuget 包导入。
  • 我们将所有内部库放在一个特定的目录中。但是当我将程序集添加到项目时,它们会添加绝对路径。我想将此路径替换为使用环境变量的路径。因此,当您在其他机器上使用该项目时,您不必更改任何内容。
  • “理想情况下作为 Nuget 包导入”并非每个开发团队都有 nuget 服务器设置。有很多理由包含内部库或不是 nuget 包的第三方包。

标签: c# visual-studio visual-studio-extensions envdte


【解决方案1】:

Microsoft.Build.BuildEngine.Project 已过时。这是一个更新的工作解决方案。

foreach (var dteProject in dte.Solution.Projects.OfType<Project>())
{
    // You can edit the project through an object of Microsoft.Build.Evaluation.Project 
    var buildProject = ProjectCollection.GlobalProjectCollection.GetLoadedProjects(dteProject.FullName).First();
    foreach (var item in buildProject.Items.Where(obj => obj.ItemType == "Reference"))
    {
        var newPath = SomeMethod(item.GetMetadata("HintPath"));

        item.SetMetadataValue("HintPath", newPath);
    }

    // But you have to save through an object of EnvDTE.Project
    dteProject.Save();
}

【讨论】:

  • 你是否偶然发布了这个扩展?
  • 我在github(github.com/thoenissen/luma)上发布了源代码,但是扩展处于早期状态。
【解决方案2】:

我创建了一个演示并在我这边重现了您的问题。我认为这是一个设计问题,如果您在环境之外修改项目,它会弹出“检测到文件修改”对话框,我们需要手动更改它。

您可以在以下链接上发布反馈:https://connect.microsoft.com/VisualStudio/Feedback 

更新:

DTE2 dte = (DTE2)this.ServiceProvider.GetService(typeof(DTE));
            EnvDTE.Project currentProject = dte.Solution.Projects.Item(1);

            // Create a new Project object.
            Microsoft.Build.BuildEngine.Project project = new Microsoft.Build.BuildEngine.Project();

            project.Load(currentProject.FullName);

            foreach (BuildItemGroup ig in project.ItemGroups)
            {
                //var items = ig.ToArray();

                foreach (BuildItem item in ig.ToArray())
                {
                    if (item.Include == "ClassLibrary1")
                    {
                        item.Include = "Utils";
                        item.SetMetadata("HintPath", @"C:\relativePath\Utils.dll");
                    }
                }
            }
            project.Save(currentProject.FullName);

【讨论】:

  • 如何更改 Demo 中的提示路径?我找不到通过 EnvDTE/VSLangProj 更改它的方法。
  • 我通过Microsoft.Build.BuildEngine.Project创建一个demo,保存项目后弹出窗口。
猜你喜欢
  • 2010-11-15
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-04
相关资源
最近更新 更多