Hiding namespaces containing only internal types in a class library?
Asked Answered
J

2

48

I have a class library that has a couple of namespaces containing only internal types.

However, when using the class library in an application project, the namespaces shows up in intellisense, but of course they are empty. Is there any way for me to hide the namespaces completely when using intellisense in other projects?

I've tried to apply EditorBrowsableAttribute to all the internal classes as well, but what I'd like to do would be to apply that to the namespace, which is of course impossible.

Or is, if I care enough about this, the only option I have to just move the types into a namespace that contains public types?

Jacklight answered 6/4, 2009 at 8:47 Comment(1)
This was kind of hard to Google...everything kept coming up for people who wanted to hide a class inside a namespace or assembly.Hilleary
C
51

It depends on how you're referencing your class library:

  • If you have the class library project contained within your solution and use a project reference, you'll always see that empty namespace via Intellisense.
  • If you're referencing a compiled dll of your class library, you won't see the namespace popping up in intellisense, provided it contains only internal members.

Try this:

namespace ClassLibrary1
{
    namespace Internal
    {
        internal class InternalClass
        {
            public int internalStuff { get; set; }
        }
    }

    namespace Public
    {
        public class PublicClass
        {
            public int publicStuff { get; set; }
        }
    }
}

If you reference this via a project reference, you'll see the empty namespace. If you reference a dll of it, you won't.

Cystitis answered 19/2, 2012 at 9:25 Comment(1)
Before reading this answer, I was starting to organize sets of classes and stuff into subfolders while keeping them in the project's root namespace, but that just really didn't feel right. I can definitely live with them only being visible with project references though, so now I'm going to start adding the sub-namespaces in.Hilleary
M
7

I've come up against this before and found no solution, only a workaround which may or may not work in your project. Instead of defining a namespace you could use an nested static class?

Mucor answered 6/4, 2009 at 8:51 Comment(3)
Yeah, I could do that, but then I'd need to rewire all references, as nested classes will need to be part of the qualifying name all over the place. ie. I'd need to write this: var x = new NotReallyANamespace.ClassName(); instead of just adding the using directive. +1 for idea though :)Jacklight
I know, it's a long way from perfect :(Mucor
Now that C# has using static, the nested static class approach has become somewhat more feasible.Barre

© 2022 - 2024 — McMap. All rights reserved.