Thursday, July 21, 2011

How To Use a .NET 4 Based DLL From .NET 2 Based Application?

Introduction

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.

using System;

namespace Net4Assembly
{
    public class MyClass
    {
        public void DoNet4Action()
        {
            Console.WriteLine("CLR version from DLL: {0}", Environment.Version);
        }
    }
}

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.

using System;

namespace Net2Assembly
{

class Program
{
static void Main(string[] args)
{
Console.WriteLine("CLR version from EXE: {0}", Environment.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:

using System;
using System.Runtime.InteropServices;

namespace Net4ToNet2Adapter
{

[ComVisible(true)]
[Guid("E36BBF07-591E-4959-97AE-D439CBA392FB")]
public interface IMyClassAdapter
{
void DoNet4Action();
}
}

And our COM visible class, which delegates its calls to the original class.


Read more: .NET Zone
QR: how-use-net-4-based-dll-net-2

Posted via email from Jasper-Net