解决不正确的项目参考GUID

解决不正确的项目参考GUID

本文介绍了解决不正确的项目参考GUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio解决方案中有一些项目,这些项目的最终结果是项目引用,其中包含所引用项目的GUID错误. (可能是由于在某个阶段重新创建了引用的项目)

I have some projects in a Visual Studio solution that have ended up with project references that include the wrong GUID for the referenced project. (Possibly due to the referenced project being recreated at some stage)

例如考虑具有以下属性的项目CoreProject.csproj:

eg. Consider a project CoreProject.csproj with the following properties:

<ProjectGuid>{93803F9C-8C65-4949-8D44-AB7A3D0452C8}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>CoreProject</RootNamespace>
<AssemblyName>CoreProject</AssemblyName>

另一个项目对此进行了引用,但是在某些时候,GUID已更改,现在不正确.

Another project includes a reference to this, but at some stage the GUID has changed and is now incorrect.

<ProjectReference Include="..\CoreProject\CoreProject.csproj">
  <Project>{5FD52517-79F8-41D2-B6F2-EA2D8A886549}</Project>
  <Name>CoreProject</Name>
</ProjectReference>

该解决方案仍然可以在Visual Studio和msbuild中正确加载和构建,但是我怀疑使用错误的GUID可能会对VS产生一些性能影响.

The solution still loads and builds correctly in Visual Studio and msbuild, but I suspect having the wrong GUIDs may have some performance impact within VS.

对于许多存在此问题的项目,解决方案非常大,我希望不必手动重新添加这些引用.是否有任何工具或宏可以修复"项目参考GUID?

The solution is quite large with many projects that have this issue, and I'd prefer not to have to re-add these references manually. Are there any tools or macros that can 'fix' up the project reference GUIDs?

推荐答案

我认为基本的控制台应用程序应该可以解决问题,就像这样:

I think a basic console app should do the trick, something like this:

using System;
using System.IO;
using System.Linq;
using Microsoft.Build.Evaluation;

public class Program
{
    public static void Main(String[] args)
    {
        var projects = Directory.EnumerateFiles(@"...", "*.csproj", SearchOption.AllDirectories)
            .Select(x =>
                    {
                        try
                        {
                            return new Project(x);
                        }
                        catch
                        {
                            return null;
                        }
                    })
            .Where(x => x != null)
            .ToList();
        var guids = projects.ToDictionary(p => p.FullPath, p => p.GetPropertyValue("ProjectGuid"));

        foreach (var project in projects)
        {
            var save = false;

            foreach (var reference in project.GetItems("ProjectReference"))
            {
                var path = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(project.FullPath), reference.EvaluatedInclude));
                if (!guids.ContainsKey(path))
                {
                    Console.WriteLine("No {0}", Path.GetFileName(path));
                    continue;
                }

                var guid = guids[path];
                var meta = reference.GetMetadataValue("Project");
                if (meta != guid)
                {
                    Console.WriteLine("{0} -> {1}", meta, guid);
                    reference.SetMetadataValue("Project", guid);
                    save = true;
                }
            }

            if (save)
                project.Save(project.FullPath);
        }
    }
}

这篇关于解决不正确的项目参考GUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 10:06