Piotr Kula
Piotr Kula

Reputation: 9841

How do I resolve a namespace conflict?

I ran into a strange problem with namespaces in a project of mine while creating test classes.

So I have a nuget package that I made and its namespace is

ppumkin.APIService

Then I created another solution for tests and the namespace on that solution is

tests.ppumkin.ControllersAPI

Problem is that I am trying to access my nuget packages dll object but visual studio is telling me that, that object does NOT exist in tests.APIService but the code I wrote is

var obj = new ppumkin.APIService.TheClass()

How to I tell Visual Studio that the object is NOT in tests.ppumkin.APIService but just in ppumkin.APIService (eg - without the prefixed test namespace)

I can overcome this problem if I use the same object creation outside of all namespaces define in the test project.

Upvotes: 0

Views: 7689

Answers (3)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239804

You can also use global:: to indicate that you're using a namespace name that starts from the global root, not some contained namespace:

var obj = new global::ppumkin.APIService.TheClass()

Although I agree that renaming your namespaces is probably a wiser long-term move.

Upvotes: 8

Piotr Kula
Piotr Kula

Reputation: 9841

From the comments I realised that my namespace for the test project is incorrect.

Following convention of top level domain of ppumkin. by prefixing it with test I am changing the entire namespace encapsulation.

The correct thing to do in my case - because I can control the namespaces and I want them to be as correct as possible is to fix the namespace of the test project to

ppumkin.Tests.
ppumkin.APIService.
ppumkin.Domain.Models.
ppumkin.Domain.Interfaces.

Now everything works as it supposed to - It was just a blind naming issue

Upvotes: 0

J. Tuc
J. Tuc

Reputation: 424

This is how you can solve your problem, create an alias for your using pointing at the namespace of your service. Alias can be then used to reference the correct namespace that contains the class that you are trying to create and instance of.

using apiservice = ppumkin.APIService;

namespace tests.ppumkin.ControllersAPI
{
    class TestClass1
    {
        void test()
        {
            var obj = new apiservice.YourClass();
        }
    }
}

namespace ppumkin.APIService
{
    class YourClass
    {

    }

}

Upvotes: 4

Related Questions