まず、.NET4.0 フレームワークでのみ利用可能な追加機能を含めたり除外したりできる条件付きコンパイル シンボルを作成することから始めることができます。
次に、VS.NET にプロジェクトをビルドさせるのではなく、MSBuild を直接使用する必要があると思います。私はかつて似たようなことをしたことがあります。つまり、MSbuild スクリプト内に 2 つのビルド タスクを作成する必要があるという事実に帰着します。.NET 3.5 用のプロジェクトをビルドできるものと、.NET 4.0 を対象とするプロジェクトをビルドできるものです。
各ビルド タスクで、ターゲット フレームワークと、使用する条件付きコンパイル シンボルを定義できます。
ビルド スクリプト内のビルド タスクは次のようになります。
<Target Name="buildall-v4">
<!-- The following ItemGroup defines all the build-constants that have to be used
in the build.
As can be seen, the DEBUG & RELEASE constants are only included when necessary -->
<ItemGroup>
<BuildConstant Include="DEBUG" Condition="'$(buildmode)'=='DEBUG'" />
<BuildConstant Include="RELEASE" Condition="'$(buildmode)'=='RELEASE'" />
<BuildConstant Include="NET_FRAMEWORK_4_0" />
</ItemGroup>
<PropertyGroup>
<BuildConstantsToUse>@(BuildConstant)</BuildConstantsToUse>
</PropertyGroup>
<MSBuild Projects="$(builddir)\Source\MyProject.sln"
Properties="OutputPath=$(outputdir)\v4;Configuration=$(buildmode);DefineConstants=$(BuildConstantsToUse);TargetFrameworkVersion=v4.0" />
</Target>
<Target Name="buildall-v3.5">
<!-- The following ItemGroup defines all the build-constants that have to be used
in the build.
As can be seen, the DEBUG & RELEASE constants are only included when necessary -->
<ItemGroup>
<BuildConstant Include="DEBUG" Condition="'$(buildmode)'=='DEBUG'" />
<BuildConstant Include="RELEASE" Condition="'$(buildmode)'=='RELEASE'" />
</ItemGroup>
<PropertyGroup>
<BuildConstantsToUse>@(BuildConstant)</BuildConstantsToUse>
</PropertyGroup>
<MSBuild Projects="$(builddir)\Source\MyProject.sln"
Properties="OutputPath=$(outputdir)\v3.5\;Configuration=$(buildmode);DefineConstants=$(BuildConstantsToUse);TargetFrameworkVersion=v3.5" />
</Target>
Offcourse, when you'd want to build for the 2 versions, you'll have to execute each msbuild command separatly on the command line.