解决不正确的项目引用GUID

时间:2012-05-30 14:00:41

标签: visual-studio-2010 msbuild project-reference

我在Visual Studio解决方案中有一些项目最终导致项目引用包含引用项目的错误GUID。 (可能是由于引用的项目在某个阶段重新创建)

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

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

另一个项目包含对此的引用,但在某个阶段GUID已更改,现在不正确。

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

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

解决方案非常庞大,许多项目存在此问题,我宁愿不必手动重新添加这些引用。是否有任何工具或宏可以“修复”项目参考GUID?

1 个答案:

答案 0 :(得分:8)

我认为一个基本的控制台应用应该可以做到这一点:

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);
        }
    }
}