Targeting Multiple Framework For Single .NET Applications

Our job is to develop. Doing the same thing many times we face lots of issues.

One of them is to compile the same application in a different framework. As we know the bottom to up approach works in .Net. In case I need to compile and use 4.0 framework dll in 4.5 I can do it without any issue. But vice versa won’t work in our case. Now it’s a serious concern in case I have few common components that we are using in 2010 and 2015.To overcome this problem here is the solution.

Follow the following steps:

Step 1:
Create a new Class Library Application and select “Configuration Manager” as shown in below screen shot:

new

Step 2: Select “New” from “Debug” option as shown below:

new

Step 3: Provide the name and Project Contract as shown below:

name

Step 4: Now do the same for the release as well, click on release select new as mention below:

new

Step 5: Now the next step is to edit the .csproj file, right click on .csproj file and open in notepad, as shown below:

open

Step 6: Find below entries in the notepad :

  • <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  • <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">

Now explicitly as the below line under tag :

code

Step 7: Add below tags under Release tag,

  1. <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Framework45|AnyCPU'">  
  2. <DebugSymbols>true</DebugSymbols>  
  3. <OutputPath>bin\Debug-Net45\</OutputPath>  
  4. <DefineConstants>DEBUG;TRACE</DefineConstants>  
  5. <DebugType>full</DebugType>  
  6. <PlatformTarget>AnyCPU</PlatformTarget>  
  7. <ErrorReport>prompt</ErrorReport>  
  8. <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>  
  9. <PlatformTarget>AnyCPU</PlatformTarget>  
  10. </PropertyGroup>  
  11. <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Framework45Release|AnyCPU'">  
  12. <DebugType>pdbonly</DebugType>  
  13. <Optimize>true</Optimize>  
  14. <OutputPath>bin\Release-Net45\</OutputPath>  
  15. <DefineConstants>TRACE</DefineConstants>  
  16. <ErrorReport>prompt</ErrorReport>  
  17. <WarningLevel>4</WarningLevel>  
  18. <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>  
  19. <PlatformTarget>AnyCPU</PlatformTarget>  
  20. </PropertyGroup> 
As shown below:

code

I think we are done, let see the output below.

Output:

Let’s have a look to the bin folder,

folder

Build your application in “Debug, Release, Framework45 and Framework45Release”. You will get different dll in different folders. Add the dll in 2010 Visual studio, in case it’s of debug or release it will support 4.0 frameworks as explicitly we mention that and other will support 2.5 frameworks.

In the below screen show I have shown that in VS 2010, I am using VS 2015 compiled DLL. And it’s working absolutely fine.
 
code

Read more articles on .NET:

Up Next
    Ebook Download
    View all
    Learn
    View all