25

Are there compatibility barriers with a .NET 4.0 assembly calling code in a .NET 2.0 assembly? And vice versa?

More specifically, I'm investigating an upgrade to Visual Studio 2010 when using a third party application based on .NET 2.0. The application is extensible by hooks that reference my custom code. And vice versa, my code will reference the application's assemblies.

spoulson
  • 20,523
  • 14
  • 72
  • 101
  • 1
    Have a look at [this question](http://stackoverflow.com/questions/1456785/a-definite-guide-to-api-breaking-changes-in-net). – Igby Largeman Apr 15 '10 at 16:46

2 Answers2

38

The CLR, in .NET 4, can consume .NET 2 assemblies and use them properly.

If you want your .NET 2 application to be able to load .NET 4 assemblies, you'll need to configure it differently. By setting the requiredRuntime to .NET 4, and the legacy load policy, you should be able to force the .NET 2 application to load using CLR 4, which would allow your .NET 4 assemblies to be used.

Setup your app.config file to include:

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

That being said, in a situation like this, I'd recommend just using VS 2010 and targetting .NET 3.5 instead of .NET 4. This would compile your assemblies for CLR 2, and avoid this issue entirely.

Reed Copsey
  • 522,342
  • 70
  • 1,092
  • 1,340
-3

.NET 4.0 assemblies can call .NET 2.0 assemblies with no difficulty. However, it is highly unlikely you will be able to call a .NET 4.0 assembly from .NET 2.0 unless the .NET 4.0 assembly did not take advantage of any new framework features and you setup assembly redirects.

Tom Cabanski
  • 7,390
  • 2
  • 20
  • 25