15
votes

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?

1
You will want to read this: marcgravell.blogspot.nl/2012/09/… too!!sehe
@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.nuzzolilo
@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.nuzzolilo
@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
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

1 Answers

12
votes

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.