11

I taught myself coding, and I'm not sure if this is possible. Neither do i know if what I ask here goes by some name (e.g.: "What you ask is called xxxxxx"). I couldn't find anything on this topic (but did find some upgrade articles, which is not exactly what I want, so please excuse me if this sounds like a NOOB question to hardcore coders; I'm a beginner).

I had a small project that relied on .NET 2.0 because of inclusion of some external libraries. The software worked well, but now it needs added functionality; things that would be much more easy to program under .NET 4.0 or 4.5.

However, that included external library isn't at that .NET level, so now I wonder: can a project have multiple .NET versions ?

I'm not sure but was thinking also perhaps I only write my new function as a dll that depends on .NET 4.5, in which I write my public functions in a different project, then later include that final dll in my prj that depends on .NET 2.0... not sure if this would be the way to go though.

Lunivore
  • 17,277
  • 4
  • 47
  • 92
user613326
  • 2,140
  • 9
  • 34
  • 63
  • From what I've seen in Microsoft repositories, they have separate projects for each .NET version they want to support.... – Callum Linington Jun 02 '15 at 10:39
  • Shouldn't you be able to just use any library that depends on a lower (or equal) .Net version? – germi Jun 02 '15 at 10:40
  • 9
    With the exception of mixed-mode assemblies, I'd expect assemblies built on .NET 2.0 to usually just work in .NET 4.0 or 4.5... – Jon Skeet Jun 02 '15 at 10:40
  • Better way of looking at this is c# backwards compatible. Almost is the best answer I think as its been asked before. http://stackoverflow.com/questions/2843318/is-c-sharp-4-0-backward-compatible-to-c-sharp-2-0 or http://stackoverflow.com/questions/5084921/rules-for-c-sharp-class-backward-compatibility-avoiding-breaking-changes – ShufflerShark Jun 02 '15 at 10:41

4 Answers4

7

Yes, you can include .NET 2 assemblies into a .NET 4 or .NET 4.5 project (or any version if you will). Including a .NET 4 assembly in a .NET 2 project won't work.

If you want to use new features in a base project, you need to upgrade all projects that rely on it. Make sure that the entire tree supports .NET 4 then (for example Office add-ins, or other related software you might use).

An exception is the use of mixed-mode assemblies (assemblies that use both .NET and unmanaged code), which are more tight to the CLR and they might cause problems due to the activity policy (MSDN).

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • V2 mixed-mode assemblies can be made to work under V4 runtime with useLegacyV2RuntimeActivationPolicy: https://msdn.microsoft.com/en-us/library/bbx34a2h.aspx – Jonathan Dickinson Jun 02 '15 at 11:00
  • @JonathanDickinson: Indeed. See the link under the activation policy. Added the MSDN link. – Patrick Hofman Jun 02 '15 at 11:00
  • Do you mean then that my main app has to be converted to v4 while i have to keep routines in v2 in some kind of special policy thats part of v4 ? – user613326 Jun 02 '15 at 11:18
  • @user613326: No, if you intend to use .NET in a sub project to be included in a .NET 4 project, that is okay. The other way around it isn't. And mixed assemblies give problems too (I doubt you use it). – Patrick Hofman Jun 02 '15 at 11:19
3

You can only use one .NET version, and that's the version that your primary application requires (or is configured) to use. However, .NET is largely backwards compatible with older versions and can often run older assemblies unchanged in newer versions of the framework.

So even though your app may be running .NET 4 or 4.5, you can use assemblies (or libraries) that were written for .NET 2 (although some restrictions may apply, as others have mentioned). Those assemblies just run under the 4.5 CLR (assuming there are no backwards compatibility issues with them), which is rare but does happen).

But the key to remember, your running application must be at the highest version of any contained assemblies. Meaning, if you have 4.5 assemblies, you can't run your app as a 2.0 app. It must be the other way around.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
1

It sounds like you are looking for Side-by-side execution, especially In-Process Side-by-Side execution:

In-Process Side-by-Side execution

Oliver
  • 1,507
  • 1
  • 12
  • 23
0

yes, you can include. that's what CLS Do

If you want to use new features in a base project, you need to upgrade all projects that based on it.

Shian JA
  • 848
  • 4
  • 15
  • 52