English 中文(简体)
C#.NET Namespace name does not exist in namespace error - only when using is outside local namespace directive - why?
原标题:

Using .NET 2.0, C#, Windows Forms development, Enterprise Library 3.1.

We have a project namespace (call it Project). We also have several sub-namespaces inside of that project, for example Project.Namespace1, Project.Namespace2, etc.

In one class, we define enums and such to be used with the Enterprise Library Logging block, like this:

namespace Project.Logging
{
  public static class Logging
  {
    public enum LogPriority
    {
      // enum values here
    }
  }
}

In another class, I use the enum values so I need to declare a using statement. Same project, so there is no assembly to reference, right?

If I declare the using inside of the local namespace, like this, it works fine:

namespace Project.SomeName
{
  using Project.Logging;

  // code referencing the Logging enum
}

However, if I put the using statement outside of the local namespace declaration, I get the "type or namespace name LogPriority does not exist in the namespace Project.Logging ... Like this:

using Project.Logging;

namespace Project.SomeName
{
  // code referencing the Logging.LogPriority.whatever
}

Why is this? Has anyone run across this before?

最佳回答

I have run into similar (though not exactly the same) problems before when using a class that has the same name as its namespace.

Oddly enough it seemed to compile ok on some developers pc s but not on others. In the end we made sure that no namespace contained a class of the same name.

namespace Project.Logging
{  
  public static class Logging  // this is what caused the probems for me
  {   

  }
}
问题回答

I also had a wired error. I cannot find any namespace which is coming from different assemblies, but begins with executing assembly name. Finally, I found out that I have set the target framework to .NET framework client profile.

Yes, most likely you have an unusual value set for the "Default Namespace" in your project properties. I would validate the project configuration.

We ran into this issue before and it all went down to ambiguous naming of the namespace and the class name.

When we tried to have our namespace as Services.Web.xxx and also add in a service reference as Services.Web.xxxx and ALSO add a references to an assembly that was named Services.Web.xxx you can only imagine the problems we ran into.

In the end to fix it we simply did a rename to make sure that there was only one instance of the Services prefix

Also you could do the following and create an alias to LogPriority to LogEnum:

using LogEnum= Project.Logging.Logging.LogPriority;

namespace Project.SomeName
{
    internal class MyClass
    {
        public MyClass()
        {
            LogEnum enum1 = LogEnum.None;
        }
    }
}
namespace Project.Logging
{
    public static class Logging
    {
        public enum LogPriority
        {
            None,
            Default
        }
    }
}

It definitely can make a difference if you have usings inside or outside the namespace. There is a good discussion here, and it is likely to be related to your default namespace settings.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签