c# - Visual Studio C# 项目中的自定义生成操作属性

标签 c# visual-studio-2015 msbuild

在 Visual Studio C++ 项目中,我可以定义自定义构建操作和相应的属性规则(请参阅 MSBuild 文件夹下的 cl.xml)。如何在 C# 项目中为自定义生成操作创建这样的规则?在 MSBuild 文件夹下有一个文件 Microsoft.CSharp.CurrentVersion.targets,它引用了 CSharp.ProjectItemsSchema.xamlCSharp.xamlCSharp.BrowseObject.xaml 看起来非常像我需要的定义。使用 procmon 我可以看到这些文件根本没有被访问。定义自定义构建操作的正确方法是什么?

为了阐明我想要完成的事情,这里有一个例子:

  1. 我向项目添加一个图像文件 (.png)
  2. 我有一个特殊的图像资源“编译器”来转换图像(这个编译器是使用 msbuild 目标定义的)
  3. 我想更改图像文件的属性(例如目标分辨率、格式等)。在 C++ 项目中,我可以为此打开文件属性对话框。 这些属性保存为 MSBuild 项元数据并转发到 MSBuild 目标。

生成的项目文件将包含如下数据:

<ItemGroup>
    <MyImageContent Include="Image.png">
      <OutputName>MyImage.png</OutputName>
      <TargetResX>512</TargetResX>
      <TargetResY>256</TargetResY>
    </MyImageContent>
  </ItemGroup>

自定义 MSBuild 目标可以轻松使用此附加元数据。到目前为止,我知道该怎么做。

但是:在 VC++ 项目中,可以在 Visual Studio 提供的属性窗口中轻松编辑此元数据。 Visual Studio 使用的定义来自类似于我提到的 cl.xml 的规则 xml 文件。我如何在 C# 项目中完成此操作?

最佳答案

What's the correct way to define custom build actions?

您可以使用 MSBuild Targetsimported .targets file在项目文件中定义自定义构建操作。

1.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> 

2.I have a special image resource 'compiler' the transform the image (this compiler is defined using a msbuild target)

对于此构建操作,您可以导入该 msbuild 目标。

3.I want to change properties of the image file.

在 C# 项目中,图像的属性也保存为 MSBuild 项元数据并转发到 MSBuild 目标。

enter image description here

更新:

  1. 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?

据我所知,我们无法使自定义项目具有可在属性窗口中编辑的附加元数据。这仅适用于 C++ 和 C++/CLI 项目。 .NET 项目(C# 和 Visual Basic)没有那么多可供调整的选项。可以引用Sharing Project Properties in Visual C++了解更多详情。

希望对您有所帮助。

关于c# - Visual Studio C# 项目中的自定义生成操作属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43283613/

相关文章:

c++ - 使用 Visual Studio 将现有文件条目移动到项目过滤器文件夹

.net - 如何创建可部署到 Azure 的 ZIP 包

c# - MSBuild 后期构建

c# - 将字符串数组发布到 .net-core mvc

c# - 从项目内的注释或 XML 文档生成 WCF 实现/使用文档

c# - 处理多个表单之间的数据

visual-studio-2015 - aurelia Visual Studio 2015 智能感知

angularjs - Visual Studio 中的 Angular 单元测试失败

configuration - RunCodeAnalysis = true在命令提示符(MSBuild)中不起作用

c# - DSL 与方法调用 : pros and cons