Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 814759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:34:22+00:00 2026-05-15T01:34:22+00:00

There are a few minor places where code for my project may be able

  • 0

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.

Something like:

#if NET40
using FooXX = Foo40;
#elif NET35
using FooXX = Foo35;
#else NET20
using FooXX = Foo20;
#endif

Do any of these symbols come for free? Do I need to inject these symbols as part of the project configuration? It seems easy enough to do since I’ll know which framework is being targeted from MSBuild.

/p:DefineConstants="NET40"

How are people handling this situation? Are you creating different configurations? Are you passing in the constants via the command line?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:34:23+00:00Added an answer on May 15, 2026 at 1:34 am

    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>
    

    And in one of your default configurations:

    <Framework Condition=" '$(Framework)' == '' ">NET35</Framework>
    

    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.

    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>
    

    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.

    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' " />
    

    or even references

    <Reference Include="Some.Assembly" Condition=" '$(Framework)' == 'NET20' " >
      <HintPath>..\Lib\$(Framework)\Some.Assembly.dll</HintPath>
    </Reference>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It looks like there a few working solutions for using custom true type fonts
in my page there are few hidden field which are filled with the correct
I have a webpage where there are few text fields to be filled up
I'm converting an old VB form to .NET, and there a few Buttons which
I know how to use locks in my app, but there still few things
I have an application that's locked down using forms authentication. There are few nightly
There are a few examples on unobtrusive events for pure JS. Most of questions
There are a few pages here addressing usage of jQuery with .net masterpages -
There are a few windows service projects in our codebase that follow this model,
There are a few factory methods in Google Guava to create InputSuppliers, e.g. from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.