1
votes

I have a few using statements in my class. ReSharper says I need to add another namespace so I let it add it. It adds it at the top, outside the namespace declaration and we are all good.

But StyleCop says that the usings need to be INSIDE the namespace declaration. So I move the using statement inside, but then when I hover over the using statement, it says it's a different namespace. Example below:

using Owin; // when I hover, it shows up as "Owin", which is correct

namespace MyCompany.Sales.Web
{
    using System.Configuration;
    using System.Web.Http;

After moving it down, it looks like this:

namespace MyCompany.Sales.Web
{
    using System.Configuration;
    using System.Web.Http;
    using Owin; // but when I hover, it shows up as "MyCompany.Owin"

How do I stop visual studio from thinking it needs the "MyCompany" part of the namespace. Keep in mind, there IS a namespace called "MyCompany.Owin", but I do NOT have a reference to it in the project.

1
What does the compiler say?Lasse V. Karlsen
@LasseV.Karlsen When the using is inside the namespace, the compiler says type or namespace does not exist, are you missing as assembly reference?ganders
Try using global::Owin; though I'm not sure the global:: modifier can be used with namespaces.Lasse V. Karlsen
@LasseV.Karlsen bingo! Add that as an aswer so I can give you credit!ganders

1 Answers

2
votes

I don't know why this happens or how to otherwise avoid it but one way would be to instruct the C# compiler to start at the global namespace before trying to resolve the name.

You would simply prefix the namespace with the global:: modifier (yes, that's two colons in there), like so:

using global::Owin;

You can use this "trick" on types as well, assuming you have a close-by type that has a name that matches something else and no matter which combination of namespaces and whatnot the compiler still thinks it should use your closer type (usually this suggests that you have a bad name but that's a different issue):

using (var stream = new global::System.IO.FileStream(...))