Domanda

How can I pass a custom MsBuild property to Visual Studio build engine from Visual Studio Extension (I want to write a custom add-in) ? Just like Visual Studio itself is passing properties like $(Configuration) and $(Platform), I would like to attach to a build process and pass my own property (e.g. $(MyCustomProperty) = "foobar"). I know it can be accomplished using MsBuild command line (/p:Property), but I want to do it from an extension.

Here is an example of what I would like to accompilsh: a textbox on visual studio toolbar, where user can type in some text - a value, that I would like to pass to build engine.

Then in .csproj:

<PropertyGroup>
  <MyCustomProperty Condition=" '$(MyCustomProperty)' == '' ">DefaultValue</MyCustomProperty>
</PropertyGroup>

<Target Name="AfterBuild" Condition=" '$(MyCustomProperty)' = 'DoAfterBuild' ">
  ...
</Target>

So target AfterBuild would be exectued only if user typed DoAfterBuild into my extension's text box

È stato utile?

Soluzione

You can request service IVsBuildPropertyStorage, it has following method:

SetPropertyValue: Used by a project subtype to set an MSBuild property value.

Something like this:

EnvDTE.Project proj = ...;
var sol = Package.GetGlobalService(typeof(VsSolution)) as IVsSolution;
IVsHierarchy hier;
sol.GetProjectOfUniqueName(p.UniqueName, out hier);
var storage = hier as IVsBuildPropertyStorage;
storage.SetPropertyValue(..);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top