最好的方法之一是在项目中创建不同的构建配置:
<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都会为您提供一个单独的程序集。
然后创建一个AfterBuild目标以编译您的不同版本:
<Target Name="AfterBuild"> <MSBuild Condition=" '$(framework)' != 'NET20'" Projects="$(MSBuildProjectFile)" Properties="framework=NET20" RunEachTargetSeparately="true" /></Target>
此示例将在第一次构建后将framework变量设置为NET20的情况下重新编译整个项目(同时编译并假定第一次构建是上述默认的NET35)。每个编译将具有正确设置的条件定义值。
通过这种方式,如果您希望不必#ifdef这些文件,甚至可以排除项目文件中的某些文件:
<Compile Include="SomeNet20SpecificClass.cs" Condition=" '$(framework)' == 'NET20' " />
甚至参考
<Reference Include="Some.Assembly" Condition="" '$(framework)' == 'NET20' " > <HintPath>..Lib$(framework)Some.Assembly.dll</HintPath></Reference>



