Невозможно получить обновленное свойство проекта из vcxproj, используя msbuild api - PullRequest
0 голосов
/ 12 марта 2020

Я пытаюсь получить значение "Оптимизация" из vcxproj, используя msbuild API. Я получаю значение, но если я изменяю значение в Visual Studio, я не получаю обновленное значение, а продолжаю получать «Отключено». Я пытался сохранить, выходя из Visual Studio, но это не работает. Мой код основан на вопросе ниже. Есть идеи?

https://social.msdn.microsoft.com/Forums/vstudio/en-US/3662cd6b-53a3-4645-9082-fffef833ad9d/is-it-possibile-to-use-the-msbuild-api-to-parse-solution-and-project-configuration?forum=vsx

  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Reflection;
  using System.Text;
  using System.Xml;
  using Microsoft.Build.BuildEngine;
  using Microsoft.Build.Evaluation;
  using Project = Microsoft.Build.Evaluation.Project;

  namespace VCXProjVerifier
  {
     class Program
     {
        static private Dictionary<String, String> _projectToStatus = new Dictionary<String, String>();

        static Program()
        {
           _projectToStatus["First.vcxproj"] = "NotFound";
           _projectToStatus["Second.vcxproj"] = "NotFound";
        }
        public Program()
        {
        }

        static void Main(string[] args)
        {

           Assembly executingAssembly = Assembly.GetExecutingAssembly();
           string executingAssemblyLocation = Path.GetDirectoryName(executingAssembly.Location);
           string solutionPath = args[0];
           string wrapperContent = SolutionWrapperProject.Generate(solutionPath, toolsVersionOverride: null, projectBuildEventContext: null);
           byte[] rawWrapperContent = Encoding.Unicode.GetBytes(wrapperContent.ToCharArray());
           using (MemoryStream memStream = new MemoryStream(rawWrapperContent))
           using (XmlTextReader xmlReader = new XmlTextReader(memStream))
           {
              Project proj = ProjectCollection.GlobalProjectCollection.LoadProject(xmlReader); 
              foreach (ProjectItem referencedProjectNode in GetOrderedProjectReferencesFromWrapper(proj))
              {
                 string referencedProjectPath = Path.Combine(Path.GetDirectoryName(solutionPath), referencedProjectNode.EvaluatedInclude);
                 Project referencedProject = ProjectCollection.GlobalProjectCollection.LoadProject(referencedProjectPath);
                 String vcxProjFileName = Path.GetFileName(referencedProject.FullPath);
                 String status;
                 if (_projectToStatus.TryGetValue(vcxProjFileName, out status))
                 {
                    _projectToStatus[vcxProjFileName] = "Found";
                    foreach (ProjectItem anItem in referencedProject.AllEvaluatedItems)
                    {
                       if ("ProjectConfiguration" == anItem.ItemType && "Release|x64" == anItem.EvaluatedInclude)
                       {
                          foreach (var aMetaData in anItem.Metadata)
                          {
                             foreach (var conditioned in aMetaData.Project.AllEvaluatedItemDefinitionMetadata)
                             {
                                if ("ClCompile" == conditioned.ItemType && "Optimization" == conditioned.Name)
                                {
                                   _projectToStatus[vcxProjFileName] = conditioned.EvaluatedValue;
                                   break;
                                }
                             }
                          }
                       }
                    }
                 }
              }
              ProjectCollection.GlobalProjectCollection.UnloadAllProjects();
           }
           foreach (var key in _projectToStatus.Keys)
           {
              if ("Full" != _projectToStatus[key])
              {
                 Console.Write("*** ");
              }
              else
              {
                 Console.Write("    ");
              }
              Console.WriteLine(key + ":" + _projectToStatus[key]);
           }

        }
        static IEnumerable<ProjectItem> GetOrderedProjectReferencesFromWrapper(Project solutionWrapperProject)
        {
           int buildLevel = 0;
           //while (true)
           {
              var strBuildLevel = String.Format("BuildLevel{0}", buildLevel.ToString());
              var items = solutionWrapperProject.AllEvaluatedItems; // GetItems(strBuildLevel);
              if (items.Count == 0)
              {
                 yield break;
              }
              foreach (var item in items)
              {
                 yield return item;
              }
              buildLevel++;
           }
        }
     }
  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...