在MSBuild任务中,如何访问“Project”实例?

时间:2022-09-02 02:14:24

I want to get access to an intance of this. http://msdn.microsoft.com/en-us/library/microsoft.build.buildengine.project_members(v=vs.85).aspx

我希望能够访问这个内容。 http://msdn.microsoft.com/en-us/library/microsoft.build.buildengine.project_members(v=vs.85).aspx

From within an MSBuild task

从MSBuild任务中

2 个解决方案

#1


1  

you can reference macros in your build file for the project as described here: http://msdn.microsoft.com/en-us/library/c02as0cs.aspx

您可以在项目的构建文件中引用宏,如下所述:http://msdn.microsoft.com/en-us/library/c02as0cs.aspx

the project class you are referencing above is part of the api for Microsoft.Build.BuildEngine that can be programmed from a .net program

您上面引用的项目类是Microsoft.Build.BuildEngine的api的一部分,可以从.net程序编程

feel free to better clarify what you're trying to accomplish

随意更好地澄清你想要完成的事情

#2


0  

I believe the accepted answer to this post also answers this question well.

我相信这篇文章的公认答案也很好地回答了这个问题。

Code copied from linked 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);

I found this technique useful when I needed to access project macro values and project settings from within an MSBuild task that validates the project settings: I preferred to query for needed info, instead of passing macro values via args.

当我需要从验证项目设置的MSBuild任务中访问项目宏值和项目设置时,我发现这种技术很有用:我更喜欢查询所需的信息,而不是通过args传递宏值。

#1


1  

you can reference macros in your build file for the project as described here: http://msdn.microsoft.com/en-us/library/c02as0cs.aspx

您可以在项目的构建文件中引用宏,如下所述:http://msdn.microsoft.com/en-us/library/c02as0cs.aspx

the project class you are referencing above is part of the api for Microsoft.Build.BuildEngine that can be programmed from a .net program

您上面引用的项目类是Microsoft.Build.BuildEngine的api的一部分,可以从.net程序编程

feel free to better clarify what you're trying to accomplish

随意更好地澄清你想要完成的事情

#2


0  

I believe the accepted answer to this post also answers this question well.

我相信这篇文章的公认答案也很好地回答了这个问题。

Code copied from linked 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);

I found this technique useful when I needed to access project macro values and project settings from within an MSBuild task that validates the project settings: I preferred to query for needed info, instead of passing macro values via args.

当我需要从验证项目设置的MSBuild任务中访问项目宏值和项目设置时,我发现这种技术很有用:我更喜欢查询所需的信息,而不是通过args传递宏值。