I recently had cause to reference two libraries which both contained the same class file. The Namespace : Class where therefore ambiguous within my application. Interestingly enough the .Net compiler simply takes it’s bat and ball home and does not allow you to access any of the exported types from these assemblies.

To resolve this you need to create an alias for at least one of the dlls. You do this in the Reference Properties:

  1. Add references in your app to the dlls.
  2. Right click on one of the assembly entries in the References list and click Properties.
  3. In the properties windows you will see an ‘Aliases’ property with the value of ‘global’. Change this to be ‘global, myalias’.

Click build and everything should be OK except when you try to use the ambiguous class. I.e. the .Net compiler will now be happy about the scope of all of the other exported items.

To use the actual ambiguous class you need to add an extern alias:

namespace MyApp
{
    extern alias myalias;
    public class MyClass
    {
        var myClass = new myalias::AmbiguousNamespace.AmbiguousClass();
    }
}

Note: you could prefix every declaration using the ‘global::’ scope, but that is the default so we don’t need to bother.

#

posted on Thursday, December 18, 2014 1:58 PM
Filed Under [ C# .Net ]

Comments

Gravatar
# re: Ambiguous Classes in C#
posted by Dorababu
on 12/22/2014 3:52 AM
Hi I have create as suggested but I am getting the following build errors can you help me

Invalid extern alias for '/reference'; '‘global' is not a valid identifier Invalid extern alias for '/reference'; 'myalias’' is not a valid identifier The extern alias 'myalias' was not specified in a /reference option
Gravatar
# re: Ambiguous Classes in C#
posted by bconlon
on 12/29/2014 2:58 PM
You don't need the quotes. Should be just:
global, myalias
Gravatar
# re: Ambiguous Classes in C#
posted by natalie
on 1/6/2016 7:31 AM
did anybody solve this issue?
i'm still getting this error:
Compiler Error Message: CS0430: The extern alias 'MyAlias' was not specified in a /reference option
Gravatar
# re: Ambiguous Classes in C#
posted by Artiom
on 3/29/2016 3:01 PM
Thx, man, worked just fine

Post A Comment
Title:
Name:
Email:
Comment:
Verification: