2010-05-05 3 views

답변

3

는 :

using Microsoft.Build.Evaluation; 
using Microsoft.Build.Utilities; 

namespace MSBuildTasks 
{ 
    public class GetAllProperties : Task 
    { 
    public override bool Execute() 
    { 
     Project project = new Project(BuildEngine.ProjectFileOfTaskNode); 
     foreach (ProjectProperty evaluatedProperty in project.AllEvaluatedProperties) 
     { 
     if (!evaluatedProperty.IsEnvironmentProperty && 
      !evaluatedProperty.IsGlobalProperty && 
      !evaluatedProperty.IsReservedProperty) 
     { 
      string name = evaluatedProperty.Name; 
      string value = evaluatedProperty.EvaluatedValue; 
     } 

     // Do your stuff 
     } 

     return true; 
    } 
    } 
} 
6

앞의 예는 파일을 프로젝트 잠 깁니다. 이로 인해 문제가 발생할 수 있습니다. 예를 들어, 동일한 프로젝트 파일에서 여러 번 작업을 호출하는 경우. 개선 된 코드는 다음과 같습니다.

using System.Xml; 
using Microsoft.Build.Evaluation; 
using Microsoft.Build.Utilities; 

namespace MSBuildTasks 
{ 
    public class GetAllProperties : Task 
    { 
    public override bool Execute() 
    { 
     using (XmlReader projectFileReader = XmlReader.Create(BuildEngine.ProjectFileOfTaskNode)) 
     { 
     Project project = new Project(projectFileReader); 

     foreach (ProjectProperty property in project.AllEvaluatedProperties) 
     { 
      if (property.IsEnvironmentProperty) continue; 
      if (property.IsGlobalProperty) continue; 
      if (property.IsReservedProperty) continue; 

      string propertyName = property.Name; 
      string propertyValue = property.EvaluatedValue; 

      // Do your stuff 
     } 

     return true; 
     } 
    } 
    } 
} 
관련 문제