条件编译与框架目标 [英] Conditional Compilation and Framework Targets

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

问题描述

有一些小的地方,code为我的项目也许能够得到显着改善,如果目标框架是一个较新的版本。我希望能够更好地利用条件编译在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? Seems easy enough to do since I'll know which framework is being targeted from MSBuild.

/p:DefineConstants="NET40"

更新:我的问题是如何在人们处理这种情况?你创建不同的配置?您是否想在常量通过命令行传递?

Update: My question is 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>

和在默认配置:

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

这个例子将重新编译设置为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.

在这种方式,你甚至可以排除某些文件在项目文件中,如果你想W / O不必支持#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天全站免登陆