3

Is it possible, and if yes then how can I do it, to create a .NET project (in C#) which can be compiled both for 2.0 and 4.0 .NET.

What I need:

I'd like to create a project with some features available in C# 4.0, but using these features is optional. If the project is compiled for .NET 4.0, the features are active and compiled in. When compiling for 2.0 the features are turned out and omitted.

I'd like to have one building process, which means when I click the "build" button in VS the project is built for both 2.0 and 4.0 creating separated assemblies and executables.

How can I configure my project and my code to achieve this requirement?

Victor
  • 151
  • 5
  • 1
    The following link shows how you can do it: http://stackoverflow.com/questions/2923210/c-sharp-conditional-compilation-and-framework-targets – s1cart3r Jun 26 '13 at 11:08
  • It takes 10 minutes to install .NET 4 on a machine. It is free and doesn't take any of your time. Very competitive with the solution you are contemplating, one that you'll regret for much longer than 10 minutes. – Hans Passant Jun 26 '13 at 11:33

3 Answers3

1

You can place your code which only compiles in .NET 4 in seperate dll, load the relevant classes, methods etc.. by reflection.

So let's say you have a method which "sometimes" need to start a new task= You should put the task hadeling in seperate dll, if the version is suitable, you can than load the class from the dll and run its method by reflection.

you should compile your code in .net2 only and the external dll in .net4 only.

In runtime you decide if you load the .net4 dll or not based on a configuration value, or if you want by checking if the computer has .net i installed on it: Is there an easy way to check the .NET Framework version?

Community
  • 1
  • 1
omer schleifer
  • 3,897
  • 5
  • 31
  • 42
1

How I do this is with #define's. I have two C# projects. One named Assembly-2.csproj and one named Assembly-4.csproj. The -2 project has a Conditional compilation symbol (this is stored in the Build project settings) named NET2 and the -4 project has one named NET4. Then, in the code I do the following:

#if NET2
public static void MyExtensionMethod(string self)
#else
public static void MyExtensionMethod(this string self)
#endif
{
    // ...

If you use this mechanism, be sure to rename the assembly names in the Application. In this case, I would have called the assemblies resp. Assembly-2 and Assembly-4. You can also skip this step, but I find it a lot easier to actually manage the assemblies if they have different names and the name shows what version of the framework they are for.

Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111
0

If you use .Net 2.0, you can compile your code with other versions (3.5, 4.0, 4.5) because newer version already includes older versions (but not removed functions-bugs)!

EDIT: But If you use .Net 4.0, you can't compile it in older versions like 2.0 because some specific functions in 4.0 aren't avaible in 2.0...

  • Don't you think you have written some thing opposite . Can you compile .NET 4.0 code in 2.0 Framework ?? I am in doubt – Rajeev Kumar Jun 26 '13 at 10:46