Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference different assemblies with the same name?

I have 2 different assemblies with the same name I need to add to my project.

When I try to add both references to the project, visual studio tells me the assembly is already referenced (because there is already an assembly with the same name).

I tried renaming one of the files. I could add both references but then, when accessing methods from the renamed assembly, resolving it fails (because .net tries to load the assembly with the original name).

Note: The assemblies are not mine, therefore I cant change their contents.

like image 803
Francisco Silva Avatar asked Aug 16 '10 09:08

Francisco Silva


2 Answers

If they are strongly signed and have different public keys maybe you can try this:

Have a separate library project for wrapping one of the two assemblies. That way you can choose a different name and namespace for one of them and then reference one of the two assemblies directly and the other indirectly.

The challenge will be that if both assemblies have the same file name you cannot have them in the same folder during runtime. As they are strongly signed you can put them both into the Global Assembly Cache (GAC) or you can put one of the two in a subfolder during runtime and add that subfolder to the probing path.

Good luck!

like image 166
Manfred Avatar answered Sep 27 '22 21:09

Manfred


Another option is to use ildasm to disassemble one of the assemblies and then use ilasm to assemble it back with a different name.

For example, to add 2 reference to the same assembly, on my system I do the following (windows 7, 64 bit, so adjust your paths accordingly)

d:\Sandbox>"c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\ildasm.exe" /all /out=SandboxLib2.il SandboxLib.dll

followed by

d:\Sandbox>"c:\Windows\Microsoft.NET\Framework\v4.0.30319\ilasm.exe" /dll SandboxLib2.il

After this I have another assembly called SandboxLib2.dll. Even though its the same assembly as first, I can add references to both the .dlls in my project.

like image 33
Chaitanya Avatar answered Sep 27 '22 21:09

Chaitanya