ambiguous class with namespace names in 2 dlls

前端 未结 2 1416
栀梦
栀梦 2021-02-05 08:40

I\'ve imported 2 dlls to my application (third party) Now both of them have a namespace with same name. For example A.B and in both of them there is a class again with a same na

2条回答
  •  悲哀的现实
    2021-02-05 09:24

    Let's suppose that you have 2 assemblies (ClassLibrary1.dll and ClassLibrary2.dll) that both define the same class in the same namespace:

    namespace Foo
    {
        public class Bar
        {
        }
    }
    

    Now in the consuming project you could define an additional alias in the references of the class library:

    enter image description here

    And now you could do the following to help the compiler disambiguate:

    extern alias lib1;
    extern alias lib2;
    
    class Program
    {
        static void Main()
        {
            var barFromLib1 = new lib1::Foo.Bar();
            var barFromLib2 = new lib2::Foo.Bar();
        }
    }
    

提交回复
热议问题