1

If I have an application (App) referencing a library (Lib1) which itself reference another library (Lib2), when App references Lib1, it will also copy the Lib2 dll and work fine.

If I also add another reference to App to another different library, but with the same name as Lib2 (Lib2bis), Lib1 will try to use the types from Lib2bis instead of Lib2 (as the Lib2 file, I believe, gets overwritten by the Lib2bis file (named the same).

Since the reference from App to Lib2 is indirect, I won't know that Lib2bis conflicts with Lib2 until the execution which will cause a runtime error.

Is there any way to prevent/resolve this issue (apart from renaming the libraries...) (ideally, not using alias, but I would be interested in knowing the alias solution as well if one).

user4388177
  • 2,433
  • 3
  • 16
  • 30
  • You will have to rename the library DLL. But if two assemblies use the same namespace and class names, but to represent different classes, it simply won't work. – Matthew Watson Sep 22 '15 at 09:33
  • @MatthewWatson - it's fine (for certain values of "fine") for two independent assemblies to declare types with identical fully-qualified names. That's what [`extern alias`](https://msdn.microsoft.com/en-us/library/ms173212.aspx) is designed to help resolve. – Damien_The_Unbeliever Sep 22 '15 at 09:35
  • @Damien_The_Unbeliever Aye, but not if the assemblies have the same DLL name? So he'll have to rename it AND use `extern alias`. – Matthew Watson Sep 22 '15 at 09:43
  • Give your dll's better names to avoid this situation in the first place. – David Arno Sep 22 '15 at 10:18
  • You don't always get to choose; you might be using a name for one of your assembly and the reference might be an assembly from an external company or you might have two version of the same assembly, so renaming can't always be the solution. – user4388177 Sep 22 '15 at 12:47

1 Answers1

3

You need assembly alias.

Set alias for your assembly (in my example its sanford):

enter image description here

In code:

extern alias sanford;
using System;
...

event EventHandler<sanford::Sanford.StateMachineToolkit.Data> TransitionCompleted;

Also, you need to rename one dll-file.

Backs
  • 24,430
  • 5
  • 58
  • 85
  • 1
    Aliases solve a compilation problem but don't resolve the fact that the `bin` directory can only contain one `Lib2.dll` file, which I think is the problem the OP is trying to solve. – Damien_The_Unbeliever Sep 22 '15 at 09:39
  • @Damien_The_Unbeliever if i remember right, just renaming should help – Backs Sep 22 '15 at 09:43
  • A little more detail: http://blogs.msdn.com/b/ansonh/archive/2006/09/27/774692.aspx – Matthew Watson Sep 22 '15 at 09:47
  • 1
    @Damien_The_Unbeliever you're right, it doesn't solve the renaming issue; as soon as I build/add the reference one overwrites the other. Would be nice if there was a way to tell to put a suffix instead of overwriting. – user4388177 Sep 22 '15 at 12:44