First of all, you could start off by creating a conditional compilation symbol that enables you to include or exclude the extra features that are only available for the .NET4.0 framework.
Next to that, I think you'll have to use MSBuild directly, instead of letting VS.NET build your project.
I've done something similar once. In short, it comes down to the fact that you'll have to create 2 build tasks within your MSbuild script. One that allows you to build your project for .NET 3.5, and one that allows you to build the project targetting .NET 4.0.
In each build-task, you can define the target framework, and the conditional compilation symbol that you'd want to use.
The build - tasks in your build-script could like like this:
<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.