Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the namespace alias operator :: to work under C#?

I've come up against the unlikely scenario when I reference two external assemblies that both have the same namespace and type names. When I try to use the type, the compiler throws an error that it cannot resolve which one I want to use.

I see that C# offers a mechanism to use aliases for references. You can even specify these aliases via the Property window of a reference in Visual Studio 2008. How do I use this alias in my code? As I understand, I should be using the :: operator, but it fails with the following error:

CS0432 - Alias not found

The usual . operator fails as well.

In the output window I see that the compiler gets the alias passed correctly in its command line.

Any pointers on what I may be able to try next are greatly appreciated.

like image 992
Vilx- Avatar asked Mar 06 '09 14:03

Vilx-


People also ask

What does :: mean in C#?

:: operator (C# reference)The global namespace is the namespace that contains namespaces and types that are not declared inside a named namespace. When used with the :: qualifier, the global alias always references the global namespace, even if there is the user-defined global namespace alias.

Can we create alias of a namespace?

You can also create an alias for a namespace or a type with a using alias directive.

What is namespace alias?

namespace alias definition Namespace aliases allow the programmer to define an alternate name for a namespace. They are commonly used as a convenient shortcut for long or deeply-nested namespaces.


2 Answers

extern alias alias1;
using alias1::Namespace;
like image 123
Kent Boogaart Avatar answered Sep 18 '22 12:09

Kent Boogaart


Try this:

extern alias asm1;
extern alias asm2;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            asm1.MyNs.MyClass mc1 = null;
            asm2.MyNs.MyClass mc2 = null;
        }
    }
}

And add global,asm1 to the project reference for assembly1 and global,asm2 for assembly2

like image 22
Darin Dimitrov Avatar answered Sep 22 '22 12:09

Darin Dimitrov