-1

THAT'S NOT A DUPLICATE It doesn't work with an alias

How can I reference an assembly with the same Namespace?

This is DLL1:

namespace ClassLibrary1
{
    public class Class1
    {
        public void Test()
        {
            Console.WriteLine("Test 1");
        }
    }
}

And this is DLL2 that references DLL1:

namespace ClassLibrary1
{
    public class Class1
    {
        public void Test()
        {
            new ClassLibrary1.Class1().Test(); //Should call Test() in DLL1
            Console.WriteLine("Test 2");
        }
    }
}

Obviously this will not work as expected. I already tried to set a different alias for the reference (DLL2 -> DLL1). Visual Studio seems to ignore the alias not matter what I do.

It doesn't work with an alias

myAlias::ClassLibrary1.Class1() is NOT WORKING Visual Studio (the compiler) ignores the alias. If I build and start my solution I get an endless loop.

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • 1
    You actually declare the same function twice here - don't think you can do that and have both assemblies loaded in the same domain at once. – 500 - Internal Server Error Sep 17 '19 at 11:05
  • @500-InternalServerError If they're different versions of the same assembly, you are right. But if they're two separate assemblies that just happen to have the same namespace and type, that's exactly what assembly aliases are for. – Luaan Sep 17 '19 at 11:09
  • 4
    Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](//meta.stackexchange.com/q/5221) – Machavity Sep 17 '19 at 12:54

1 Answers1

2

You need to specify the alias explicitly when refering to the type, e.g. new myAlias::ClassLibrary1.Class1(). You can also use a using alias to do something like using AnotherClass1 = myAlias::ClassLibrary1.Class1; for convenience.

Of course, if possible, you want to avoid having this problem in the first place. Choose namespace and type names to avoid collisions, if you have control over either assembly.

Luaan
  • 62,244
  • 7
  • 97
  • 116