In a Visual Studio C++ project I can define custom build actions and corresponding property rules (see cl.xml
under the MSBuild folder). How can I create such a rule for a custom build action in a C# project? Under the MSBuild folder there's a the file Microsoft.CSharp.CurrentVersion.targets
which references CSharp.ProjectItemsSchema.xaml
, CSharp.xaml
and CSharp.BrowseObject.xaml
which looks exacltly like the definitions I need. Using procmon I can see that these files are not accessed at all. What's the correct way to define custom build actions?
To clarify what I want to accomplish here's an example:
The resulting project file would contain data like this:
<ItemGroup>
<MyImageContent Include="Image.png">
<OutputName>MyImage.png</OutputName>
<TargetResX>512</TargetResX>
<TargetResY>256</TargetResY>
</MyImageContent>
</ItemGroup>
This additional metadata can easily be used by a custom MSBuild target. So far I know how to do it.
But: In a VC++ project this metadata can be easily edited in a property window provided by Visual Studio. The definition Visual Studio uses comes from a rules xml file similar to the cl.xml I mentioned. How can I accomplish this in a C# project?
What's the correct way to define custom build actions?
You can use MSBuild Targets or imported .targets file to define custom build actions in the project file.
- I add an image file (.png) to the project For this build action, you can use a copy task in the target to copy the image file to the project folder:
<Target Name="CopyFiles">
<Copy
SourceFiles="@(MySourceFiles)"
DestinationFolder="c:\MyProject\Destination"
/>
</Target>
- I have a special image resource 'compiler' the transform the image (this compiler is defined using a msbuild target)
For this build action, you can import that MSBuild target.
- I want to change properties of the image file.
In the C# project, the image properties are also saved as MSBuild item metadata and forwarded to the MSBuild target.
- In a VC++ project this metadata can be easily edited in a property window provided by Visual Studio. The definition Visual Studio uses comes from a rules xml file similar to the cl.xml I mentioned. How can I accomplish this in a C# project?
As far as I know we could not make custom items have additional metadata which be editable in the property window. This only can be applied to C++ and C++/CLI projects. .NET projects (C# and Visual Basic) don’t have that many options to be tweaked. You can refer to the Sharing Project Properties in Visual C++ for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With