条件编译和框架目标

编程入门 行业动态 更新时间:2024-10-28 14:29:06
本文介绍了条件编译和框架目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果目标框架是较新版本,我的项目的代码可能会在一些小地方得到显着改进.我希望能够更好地利用 C# 中的条件编译来根据需要切换这些.

There are a few minor places where code for my project may be able to be drastically improved if the target framework were a newer version. I'd like to be able to better leverage conditional compilation in C# to switch these as needed.

类似于:

#if NET40 using FooXX = Foo40; #elif NET35 using FooXX = Foo35; #else NET20 using FooXX = Foo20; #endif

这些符号中的任何一个是免费提供的吗?我是否需要将这些符号作为项目配置的一部分注入?这似乎很容易做到,因为我会知道 MSBuild 的目标是哪个框架.

Do any of these symbols come for free? Do I need to inject these symbols as part of the project configuration? It seems easy enough to do since I'll know which framework is being targeted from MSBuild.

/p:DefineConstants="NET40"

人们如何处理这种情况?您是否正在创建不同的配置?你是通过命令行传入常量吗?

How are people handling this situation? Are you creating different configurations? Are you passing in the constants via the command line?

推荐答案

实现此目的的最佳方法之一是在您的项目中创建不同的构建配置:

One of the best ways to accomplish this is to create different build configurations in your project:

<PropertyGroup Condition=" '$(Framework)' == 'NET20' "> <DefineConstants>NET20</DefineConstants> <OutputPath>bin$(Configuration)$(Framework)</OutputPath> </PropertyGroup> <PropertyGroup Condition=" '$(Framework)' == 'NET35' "> <DefineConstants>NET35</DefineConstants> <OutputPath>bin$(Configuration)$(Framework)</OutputPath> </PropertyGroup>

在您的默认配置之一中:

And in one of your default configurations:

<Framework Condition=" '$(Framework)' == '' ">NET35</Framework>

如果没有在其他任何地方定义,它将设置默认值.在上述情况下,每次构建每个版本时,OutputPath 都会为您提供一个单独的程序集.

Which would set the default if it wasn't defined anywhere else. In the above case the OutputPath will give you a separate assembly each time you build each version.

然后创建一个 AfterBuild 目标来编译您的不同版本:

Then create a AfterBuild target to compile your different versions:

<Target Name="AfterBuild"> <MSBuild Condition=" '$(Framework)' != 'NET20'" Projects="$(MSBuildProjectFile)" Properties="Framework=NET20" RunEachTargetSeparately="true" /> </Target>

此示例将在第一次构建后重新编译整个项目,并将 Framework 变量设置为 NET20(编译两者并假设第一次构建是上面的默认 NET35).每次编译都会正确设置条件定义值.

This example will recompile the entire project with the Framework variable set to NET20 after the first build (compiling both and assuming that the first build was the default NET35 from above). Each compile will have the conditional define values set correctly.

通过这种方式,如果您不想#ifdef 文件,您甚至可以排除项目文件中的某些文件:

In this manner you can even exclude certain files in the project file if you want w/o having to #ifdef the files:

<Compile Include="SomeNet20SpecificClass.cs" Condition=" '$(Framework)' == 'NET20' " />

甚至参考

<Reference Include="Some.Assembly" Condition="" '$(Framework)' == 'NET20' " > <HintPath>..Lib$(Framework)Some.Assembly.dll</HintPath> </Reference>

更多推荐

条件编译和框架目标

本文发布于:2023-11-12 13:52:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581685.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   条件   目标

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!