条件编译和框架目标 [英] Conditional compilation and framework targets

查看:41
本文介绍了条件编译和框架目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果目标框架是较新版本,我的项目的代码可能会在一些小地方得到显着改进.我希望能够更好地利用 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>

这篇关于条件编译和框架目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆