我希望能够访问此实例。 http://msdn.microsoft.com/en-us/library/microsoft.build.buildengine.project_members(v=vs.85).aspx
从MSBuild任务中
答案 0 :(得分:1)
您可以在项目的构建文件中引用宏,如下所述:http://msdn.microsoft.com/en-us/library/c02as0cs.aspx
您上面引用的项目类是Microsoft.Build.BuildEngine的api的一部分,可以从.net程序编程
随时更好地澄清你想要完成的事情
答案 1 :(得分:0)
我相信the accepted answer to this post也能很好地回答这个问题。
从链接帖子中复制的代码:
public static class BuildEngineExtensions
{
const BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public;
public static IEnumerable GetEnvironmentVariable(this IBuildEngine buildEngine, string key,bool throwIfNotFound)
{
var projectInstance = GetProjectInstance(buildEngine);
var items = projectInstance.Items
.Where(x => string.Equals(x.ItemType, key, StringComparison.InvariantCultureIgnoreCase)).ToList();
if (items.Count > 0)
{
return items.Select(x => x.EvaluatedInclude);
}
var properties = projectInstance.Properties
.Where(x => string.Equals(x.Name, key, StringComparison.InvariantCultureIgnoreCase)).ToList();
if (properties.Count > 0)
{
return properties.Select(x => x.EvaluatedValue);
}
if (throwIfNotFound)
{
throw new Exception(string.Format("Could not extract from '{0}' environmental variables.", key));
}
return Enumerable.Empty();
}
static ProjectInstance GetProjectInstance(IBuildEngine buildEngine)
{
var buildEngineType = buildEngine.GetType();
var targetBuilderCallbackField = buildEngineType.GetField("targetBuilderCallback", bindingFlags);
if (targetBuilderCallbackField == null)
{
throw new Exception("Could not extract targetBuilderCallback from " + buildEngineType.FullName);
}
var targetBuilderCallback = targetBuilderCallbackField.GetValue(buildEngine);
var targetCallbackType = targetBuilderCallback.GetType();
var projectInstanceField = targetCallbackType.GetField("projectInstance", bindingFlags);
if (projectInstanceField == null)
{
throw new Exception("Could not extract projectInstance from " + targetCallbackType.FullName);
}
return (ProjectInstance)projectInstanceField.GetValue(targetBuilderCallback);
}
}
// Sample useage:
string targetPath = buildEngine.GetEnvironmentVariable("TargetPath", true).First();
string intermediateAssembly = buildEngine.GetEnvironmentVariable("IntermediateAssembly", true).First();
IEnumerable<string> referencePaths = buildEngine.GetEnvironmentVariable("ReferencePath", true);
当我需要从验证项目设置的MSBuild任务中访问项目宏值和项目设置时,我发现这种技术很有用:我更喜欢查询所需的信息,而不是通过args传递宏值。