The official answer is you can’t. Even with the In-Process Side by Side execution (SxS) feature, introduced in .NET 4.
The SxS feature was intended to be used when COM is involved. For example, if you got an application that loads plugins, like outlook, and it loads 2 COM plugins, one is using .NET 4 and the other is using .NET 2.0 then it will load two versions of the CLR into the process using the new SxS feature.
What if I simply have a .NET 2 application or DLL that needs to access a .NET 4 DLL?
Personally I’ve encountered two scenarios when I had to solve this problem:
- I had a 3rd-party control that would load only in a .NET 3.5 application, but I had to use it in a .NET 4 application.
- I wanted to write a plug-in for Windows Live Writer, which must use .NET 2.0, but I needed to use in my plug-in a .NET 4 DLL.
So, what can we do if no COM is involved?
Well, simply add COM to the mixture..
The idea is that you can expose the required classes from your DLL (which uses .NET Framework X) as COM classes (using COM Interop), and then use those classes from your other DLL (which uses .NET Framework Y). Since you are crossing a COM interface, in-process SxS will kick in and work its magic.
Steps to work around the problem
Create a .NET 4 DLL
Suppose we have a .NET 4 DLL which does some .NET 4 functionality. In the attached example our .NET 4 class prints the CLR version, which should be 4. This DLL is compiled with .NET Framework 4.
Create a .NET 2 EXE
Here we create a .NET 2 EXE which will eventually call the .NET 4 DLL, currently all it does is write it’s own CLR version.
Create a .NET 4 to .NET 2 adapter
Here we create a .NET 4 DLL that exposes the same functionality we need from our original .NET 4 DLL only it exposes it in a COM-friendly way. In this example, it only needs to delegate the call to the original implementation, but in more advanced scenarios it should translate the parameters to something more COM friendly. In addition to changing the parameters the classes also implement interfaces (as required by COM) and are marked with ComVisible and Guid attributes to allow access using COM.
Here is our COM visible interface:
And our COM visible class, which delegates its calls to the original class.
Note: we could have combined Net4Assembly.MyClass and Net4ToNet2Adapter.MyClassAdapter into the same class but I wanted to keep the example general. In real life application you often can’t change the original object and thus you are forced to create a wrapper.
Add support to the adapter for registration-free COM activation
Important note: this part is not really necessary for the .NET 4 to .NET 2 interop to work. But without it you will need to start using he registry for registering your .NET COM components and most projects would rather to avoid it if possible. If this is not a problem just register your objects in the registry and move to the next step.
To add support for registration-free COM we need to create two application manifest files.
The first application manifest specifies dependent assemblies for the client executable. Note that since this manifest replaces the default .NET manifest I’ve added some extra standard manifest stuff (trustinfo), but only the first part is really needed for the registration-free COM to work. To add it, add a file to the client project named app.manifest (“Add new item” –> “Application Manifest”) and change the project properties to use this file.
Following is the content of app.manifest for the Net2Aseembly.exe client in our example:
The second application manifest, describes the COM components which are exposed in the assembly. It needs to be set as the application manifest which resides as a native Win32 resource inside the DLL.
Unfortunately, this can’t be done as easily as the previous manifest. In Visual Studio 2010, the relevant field in the project properties is disabled when the project is of type Class Library. So we must go to the Net4ToNet2Adapter.csproj file and change it ourselves. The change is easy, just add the following lines in the relevant place:
Following is the content of app.manifest for the Net4ToNet2Adapter.dll in our example:
Use our .NET 4 DLL via COM
Now all you need to do is create an instance of your .NET 4 class from your .NET 2 executable using COM:
Note: Since the interface IMyClassAdapter should be duplicated in the client, I’ve added the source file IMyClassAdapter.cs as a link to the client project.
The result of running this simple console application is:
I think the image speaks for itself.
I’ve uploaded the full source of this example to MSDN Code Gallery.
That’s it for now,