How to get the value of an MSBuild variable

孤者浪人 提交于 2020-01-06 16:24:07

问题


How can I get the value of MSBuild variable (ex. $(MSBuildToolsPath), $(MSBuildAssemblyVersion), $(TargetFrameworkIdentifier), etc.).

I need to get the value on the C# side, but I can access msbuild.exe if it's not available from the C# code.


回答1:


BuildManager and the likes are for building, only; Microsoft decided to split up the MSBuild API in different namespaces according to functionality. Once you know that it's not too hard figuring out what you need: you just need to evaluate properties, so the Project class from the Evaluation namespace seems like the proper choice. It has a Properties member exposing all properties:

var project = new Microsoft.Build.Evaluation.Project( @"my.vcxproj" );
foreach( var property in p.Properties )
{
  System.Console.WriteLine( "{0} = {1}",
    property.Name, property.EvaluatedValue );
}


来源:https://stackoverflow.com/questions/36669419/how-to-get-the-value-of-an-msbuild-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!