This sounds like a job for conditional compiler directives.
Rewrite your code with
#If NET35 ... #End If
and
#If NET40 ... #End If
From here, you need to add the compilation constants NET35 and NET40 to your project, and I would suggest first creating custom configurations in the configuration manager, such as DebugNET35 , DebugNET40 , ReleaseNET35 , and ReleaseNET40 . After creating these configurations, you can switch to each configuration and go to the advanced compilation options for your project, where you can set custom constants NET35 or NET40 , depending on the current configuration.
You can also set the target structure in this dialog box, but it will install your framework version globally. To configure a custom target structure for each configuration, follow the Pierre Steps .
After that, select the configuration and compile! I used this technique to share the same code base for the Demo and Full versions of the application.
We hope that future versions of Visual Studio will include a wireframe version that automatically detects: https://connect.microsoft.com/VisualStudio/feedback/details/113323/include-framework-version-defines-automatically
Edit: A new .NET Framework blog article discusses writing portable code libraries in Visual Studio 2010 and 2012: http://blogs.msdn.com/b/dotnet/archive/2012/07/06/targeting-multiple-platforms- with-portable-code-overview.aspx and it looks like a cleaner solution.
source share