C# – How to use two different Microsoft Interop assemblies in one project

assembliescconflictnet

I want to use two different Microsoft.Office.Interop assemblies (version 11 and 12) depending on the Office version installed on customer's machine.

I've been able to add these two asseblies to my project ( I am using them loccaly – they are in my bin folder, in two diffrent subfolders) , I used aliases on this assemblies to be able to use both of them in my project (respectively, aliases are Excel11 and Excel12).

But unfourtunately, when I am trying to build the solution I am getting warning:

No way to resolve conflict between
"Microsoft.Office.Interop.Excel,
Version=12.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c" and
"Microsoft.Office.Interop.Excel,
Version=11.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c".
Choosing
"Microsoft.Office.Interop.Excel,
Version=12.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c"
arbitrarily.

Best Answer

yeah that's gross, but understandable. I strongly recommend looking into the Open XML SDK for COM-less generation. but i understand time crunchs and other requirements so if you can't...here's what i would do.

to make sure were on the same page, here's what i'm thinking of for aliasing http://www.dotnetperls.com/extern if for some odd reason that is not working for you, there is another approach you could use below.

For cleanliness you could create a couple new projects. one for each version you need to implement, name them something version specific like Excel11, Excel12.

Then build wrapper classes for the functions you want to be able to access from each, you could get fancy dancy and build an abstract class in a separate Common Project that your wrapper classes extend, so you can reference them generically from your main code by the Abstract class name for uniformity. you just need to add references to the new projects in your main project, and select which wrapper you want based on however you are checking version, registry keys or otherwise. Hope it Helps.