*NOTE* This only works if your executable is running under the 2.0 Framework!
At work we are writing a .NET 2.0 application that has its roots back in 1.1. Some ancillary applications were also written in 1.1, and shared some common libraries; so when it came time for this application to be upgraded to 2.0 some libraries had to be left behind for compatibility reasons.
One such library has the task of loading data from databases, and was designed to read from MS Access, SQL Server, or MSDE; however in the ongoing development of our application we are scraping the MS Access use in favor of SQL Compact Edition. For those familiar with SQL CE, you’ll know it’s not compatible with the 1.1 framework–so we were faced with the issue of either duplicating the functionality of the 1.1 DLL in a 2.0 DLL, so that old applications could be left alone, or with doing massive work to overhaul the old applications and try to bring them all up to 2.0… neither option was terribly appealing (although the first option was obviously preferable).
Our project architect put the task to me, and had a plan of attack that he figured would let us make the 1.1 DLL talk to the SQL CE database via a 2.0 DLL proxy–however you’ll know that 1.1 projects cannot reference 2.0 projects. Here’s where a little trickery comes in…
For a quick recap we have: A Framework 2.0 application, which calls a 1.1 Library to retrieve data. We need: A Framework 2.0 application to call the 1.1 Library to call a 2.0 Library to retrieve the data from SQL CE.
To achieve this we added an interface to the 1.1 Library, and implemented that interface in the new 2.0 library which uses the interface to fetch the needed data from the database. The 1.1 library uses reflection to late-load the 2.0 library and instantiates it by its interface.
So the workflow is:
- 2.0 Application makes a call to the 1.1 Library
- 1.1 Library uses an Assembly.Load() call to late-load the new 2.0 Library and casts it to the 1.1’s interface definition.
- 1.1 Library then can call the Interface member, which…
- Talk through the 2.0 framework to our SQL CE database.