Simon
Simon

Reputation: 34830

From within an MSBuild task how do I get access to the "Project" instance?

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

From within an MSBuild task

Upvotes: 2

Views: 945

Answers (2)

Grumpy Curmudgeon
Grumpy Curmudgeon

Reputation: 137

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.

Upvotes: 2

Richard Logwood
Richard Logwood

Reputation: 3273

you can reference macros in your build file for the project as described here: 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

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

Upvotes: 1

Related Questions