15

Assuming the system has .NET 4.0 and .NET 4.5 installed.

Is it possible to load and work with a .NET 4.5 assembly from an assembly written targeting .NET 4.0?

Simply put, can I call Assembly.Load from .NET 4.0 code to load a .NET 4.5 targeting assembly?

Madushan
  • 6,977
  • 31
  • 79
  • 2
    You will want to read this: http://marcgravell.blogspot.nl/2012/09/iterator-blocks-missing-methods-and-net.html too!! – sehe Sep 27 '12 at 23:10
  • why not adjust the build settings on your other projects to use .NET4.5? – Shaun Wilde Sep 27 '12 at 23:26
  • 1
    @ShaunWilde Sometimes that's not possible. You could have multiple teams or applications at a company, one that has .NET 4.5, one that uses .NET 4.0. In an ideal world, this doesn't happen, but in the real world, it often does. Sometimes you need to interoperate components between the two. – marknuzz Feb 12 '14 at 02:51
  • @Nuzzolilo - Perhaps but you can "normally" load .NET 4.0 assemblies into .NET 4.5 without issue so if you can upgrade your host to .NET 4.5 your issue will probably go away. Also if it is within one company you can get/ask them to produce two assemblies one with each framework level. – Shaun Wilde Feb 12 '14 at 10:11
  • 3
    @ShaunWilde that's difficult when you have 200k+ lines of production code, written in VB.NET 2.0, which uses "query" as a variable name in just about every place imaginable. Upgrading to 3.5 or higher will think that "query" is referring to the namepsace "System.Web.Query" and will trigger compiler errors. It will take half a year to fix the errors. – marknuzz Feb 12 '14 at 19:45
  • 1
    @Nuzzolilo manually yes but I am sure a quick search and replace will cure most of the issues - tedious yes but possible; even if one had to touch every line of 200K+ I doubt it would take 1/2 year. – Shaun Wilde Feb 12 '14 at 21:27
  • Some templates aren't compatible with 4.5, so they can't be upgraded. – BrainSlugs83 Aug 26 '14 at 08:02
  • 3
    Also, if you think "touching every line of a 200K+ lines project will take less than 1/2 year" then you've never worked on a giant corporate project with a dysfunctionally large team of developers and regulations... – BrainSlugs83 Aug 26 '14 at 08:07

1 Answers1

12

Assuming a system as .NET 4.0 and .NET 4.5:

As stated in marcgravell's blog linked by sehe

4.5 is an in-place over-the-top install on top of 4.0, in the GAC; once you have installed 4.5, 4.0 runs with the 4.5 assemblies

Then calling Assembly.Load from a .NET code targeting 4.0 (compiled by a 4.0 compiler), will actually run in using the 4.5 framework implementation, so I don't see any reason why it couldn't load a 4.5 assembly.

margravell notes that problems occur when you try to run .NET 4.5 compiled code on a system with only 4.0 installed, as the implementation of the yield return/break iterators causes a missing method reference. But this shouldn't affect you.

MerickOWA
  • 7,453
  • 1
  • 35
  • 56
  • 4
    So then how come it doesn't work by default? -- What extra thing do you have to do to make it work? – BrainSlugs83 Aug 26 '14 at 08:02
  • 1
    Apparently .net-4.5 tries to behave like .net-4.0 in some cases too when this is done. For example, `System.ComponentModel.Composition` refuses to close open generic types when loaded by a .net-4.5 assembly loaded by a program initialized as .net-4.0. This bit me because I didn’t realize that the framework was running in .net-4.0 mode and I couldn’t guess that this might be the cause of different behavior. – binki Mar 13 '16 at 05:22