简体   繁体   English

我如何在 c# 中快速处理 csproj 文件

[英]How do i Quickly Process a csproj file in c#

I'm making a Pre-Compiler meaning it will read a load of files and process a few custom preprocessor directives.我正在制作一个预编译器,这意味着它将读取大量文件并处理一些自定义预处理器指令。 but no matter how much I look online I can't find a working processor for the .csproj file.但无论我在网上看多少,我都找不到 .csproj 文件的可用处理器。 I can read them and extract data.我可以阅读它们并提取数据。 but I can't process them to extract state with Configuration and platform parameters.但我无法处理它们以使用配置和平台参数提取状态。

what I mean is, if I have a csproj that adds a few preprocessor directives if on the DEBUG build and others on the RELEASE build, I want to be able to process the file so that when I build of DEBUG I correctly get all then defined Preprocessors and likewise for the RELEASE build.我的意思是,如果我有一个 csproj,如果在 DEBUG 构建中添加一些预处理器指令,而在 RELEASE 构建中添加其他指令,我希望能够处理该文件,以便在构建 DEBUG 时我正确地得到所有然后定义预处理器,同样用于 RELEASE 构建。

I'm going to be processing dotnet core projects to be specific.我将具体处理 dotnet 核心项目。

I want to get the file state after processing all conditions using the Platform and Configuration.我想在使用平台和配置处理所有条件后获取文件状态。 this includes stuff like OutputPath, AllowUnsafe, BuildTargets and such.这包括像 OutputPath、AllowUnsafe、BuildTargets 之类的东西。

The c# compiler, unlike the c++ compiler, does not have specific precompiled instruction item elements destined for the compiler like PreprocessorDefinitions .与 c++ 编译器不同,c# 编译器没有像PreprocessorDefinitions这样的编译器指定的特定预编译指令项元素。

As a suggestion, you can try to add a custom target in xxxx.csproj file to execute some msbuild task to do some preprocessing.作为建议,您可以尝试在xxxx.csproj文件中添加一个自定义目标来执行一些 msbuild 任务来进行一些预处理。 And you want to do all of these after pre-build target, you can just set AfterTargets="PrepareForBuild" to this custom target which means it runs after PrepareForBuild target.并且您想在pre-build目标之后执行所有这些操作,您可以将AfterTargets="PrepareForBuild"设置为此自定义目标,这意味着它在PrepareForBuild目标之后运行。

Suggesstion建议

Just like this:像这样:

<Target Name="CustomPreprocessor" AfterTargets="PrepareForBuild">

<Message Importance="high" Text="$(OutputPath)--$(AllowUnsafeBlocks)--$(PrepareForBuildDependsOn)" ></Message>

 ........//other msbuild task

 </Target>

1) If you want to use the configuration as a condition and execute different pre-instructions according to Debug or Release , you can use MSBuild conditions . 1)如果想以配置为条件,根据DebugRelease执行不同的前置指令,可以使用MSBuild conditions If you want to use Debug , you can refer to this:如果你想使用Debug ,你可以参考这个:

<Target Name="CustomPreprocessor" AfterTargets="PrepareForBuild" Condition=" '$(Configuration)' == 'Debug' ">

At the same time, if you also want to add palform as a parallel condition, please try this:同时,如果您还想添加palform作为并行条件,请尝试以下操作:

 <Target Name="CustomPreprocessor" AfterTargets="PrepareForBuild" Condition=" '$(Configuration)' == 'Debug' and '$(Platform)' =='Any CPU' ">

2) Since outputpath and AllowUnsafe are MSBuild properties, you can use $(outputpath) and $(AllowUnsafeBlocks) to get their values while you can use @(...) to get values of MSBuild items . 2)由于outputpathAllowUnsafe是 MSBuild 属性,您可以使用$(outputpath)$(AllowUnsafeBlocks)获取它们的值,而您可以使用@(...)获取MSBuild items 的值。

3) Basically, to execute a preinstruction in target, you can do it with a task, you can refer to this document to add any task as you want to do any operation. 3)基本上,要在target中执行一个预指令,你可以用一个任务来完成,你可以参考这个文档添加任何任务,你想做什么操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM