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

后端 未结 4 746
遇见更好的自我
遇见更好的自我 2021-01-07 23:28

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 compil

相关标签:
4条回答
  • 2021-01-07 23:50

    When you reference some assembly in project it has default alias "global". When you add another alias for that assembly, e.g. "global, AssemblyA", you will reference it like this:

    using SomeTypeAlias = AssemblyA::Company.Product.SomeType;
    

    or:

    void SomeMethod(AssemblyA::Company.Product.SomeType someType) { ... }
    
    0 讨论(0)
  • 2021-01-07 23:56

    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

    0 讨论(0)
  • 2021-01-08 00:04
    extern alias alias1;
    using alias1::Namespace;
    
    0 讨论(0)
  • 2021-01-08 00:05

    I think you need to use an extern alias. Anson Horton has a good blog on this problem and how to use extern alias to fix it.

    0 讨论(0)
提交回复
热议问题