Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing .NET 3.5 with 4/4.5 assemblies in the same process

Tags:

c#

.net

clr

I'd like to migrate a .NET 3.5 WinForms based application to the latest .NET version (4.5).

The application uses "external" components (can be thought of as plugins) that are also currently .NET 3.5 based.

I'd like to know what runtime/core libraries are used in case we convert ONLY THE APPLICATION to compile using .NET 4.5?

Should this scenario properly work? (loading .NET 3.5 assemblies in a 4.5 process)? * The plugin assemblies are loaded via reflection.

How does the CLR runtime handle such a scenario? is this a safe practice?

like image 291
lysergic-acid Avatar asked Dec 10 '12 15:12

lysergic-acid


People also ask

Can you have .NET 3.5 and 4.5 installed?

NET Framework 4.5 (or one of its point releases) runs side by side with versions 1.1, 2.0, and 3.5, and is an in-place update that replaces version 4. For apps that target versions 1.1, 2.0, and 3.5, you can install the appropriate version of . NET Framework on the target machine to run the app in its best environment.

How assemblies are implemented and used in the .NET framework?

NET Framework, you can build an assembly from one or more source code files. In . NET Framework, assemblies can contain one or more modules. This allows larger projects to be planned so that several developers can work on separate source code files or modules, which are combined to create a single assembly.

How does .NET resolve assembly references?

If the runtime determines that an assembly matches the calling assembly's criteria, it uses that assembly. When the file specified by the given <codeBase> element is loaded, the runtime checks to make sure that the name, version, culture, and public key match the calling assembly's reference.

How many types of assemblies support NET?

NET supports three kinds of assemblies: private. shared. satellite.


2 Answers

If you recompiled the main EXE of your app to target .NET 4.x or use an app.exe.config file with the <supportedRuntime> element to force CLR version 4 to get used then you'll have no trouble using both .NET 3.5 and .NET 4.0 assemblies. CLR v4 has no trouble reading 3.5 assemblies, it is backwards compatible. Not the other way around, CLR v2 can't read version 4 assemblies which is why you need the .config file if your EXE isn't targeting v4.

The only wrinkle is the dependencies that your 3.5 assembly has on old framework assemblies. It will for example ask for version 2.0.0.0 of mscorlib.dll. The CLR automatically translates those requests and replaces them with version 4.0.0.0. Which in general works just fine, the standard 4.0 framework assemblies are very compatible with the old versions.

Microsoft did however take the opportunity with 4.0 being a new side-by-side version and fixed old bugs that could not be easily fixed without risking breaking code that accidentally relied on the buggy behavior. They are very obscure bugs and it is pretty unlikely these bug fixes will byte you. You do however have to re-test your code to make sure.

like image 69
Hans Passant Avatar answered Sep 22 '22 02:09

Hans Passant


All assemblies will use types from .NET Framework which application targets.

Here is a simple test:

Project 'Net2Library' which is a .NET Framework 2.0 Class Library with following class:

using System; using System.Collections.Generic;  namespace Net2Library {     public class Class1     {         public static List<string> GetStrings()         {             var strings = new List<string>();             Console.WriteLine("From Net2Library: {0}", strings.GetType().AssemblyQualifiedName);             return strings;         }     } } 

Project 'Net4Application' which is a .NET Framework 4.0 Console Application that references Net2Library.dll and has following class:

using System; using Net2Library;  namespace Net4Application {     class Program     {         static void Main(string[] args)         {             Console.WriteLine("From Net4Application: {0}", Class1.GetStrings().GetType().AssemblyQualifiedName);         }     } } 

Console output will be:

From Net2Library: System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 From Net4Application: System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

You may also check out following resources: .NET Framework Assembly Unification Overview and Assembly Binding Redirection.

like image 35
Stipo Avatar answered Sep 19 '22 02:09

Stipo