tags:

views:

400

answers:

4

I want a method of the class: "One" ("AccessibleWithinSameNamespace") to be accessible by the class: "Two", without having "Two" extending "One".

Both classes are in the same namespace, so I'm thinking that maybe there's an access-modifier that emulates the "protected" modifyer, but for namespaces.

Some code:

namespace Test
{
    class One
    {
        public void AccessibleToAll()
        {
        }


        protected void AccessibleWithinSameNamespace()
        {
            // I am not public

            // I can be accessed from other classes
            // within the same namespace of my class
        }
    }
}

namespace Test
{
    class Two
    {
        public Two()
        {
            One one = new One();

            // I am able to access this method because my class
            // is in the same namespace as the class: "One"
            one.AccessibleWithinSameNamespace();
        }
    }
}
+4  A: 

You can use the internal modifier if both classes are in the same assembly.

With your example:

namespace Test
{
    class One
    {
        public void AccessibleToAll()
        {
        }


        internal void AccessibleWithinSameNamespace()
        {
            // I am not public

            // I can be accessed from other classes
            // within the same namespace of my class
        }
    }
}

namespace Test
{
    class Two
    {
        public Two()
        {
            One one = new One();

            // I am able to access this method because my class
            // is in the same namespace as the class: "One"
            one.AccessibleWithinSameNamespace();
        }
    }
}
Jorge Villuendas
Thanks. I will try that out.
roosteronacid
+1  A: 

C# and .NET have no concept of "within the same namespace". Internal is the closest equivalent.

Jon Skeet
+1  A: 

The problem isn't the access modifier for the namespace its the access modifier for the function. "Protected" means that it can be accessed by child classes, not by other classes even if they're in the same namespace.

You have several solutions to choose from... 1. you can make the access modifier for the function "internal" then all classes/functions inside the same assembly (or marked with some cool assembly flags so that they pretend to be in the same assembly) can access it 2. Make "TWO" a sub class of "One" then two can call into it (but not via an instance of "One" as shown in the same code

David Heise
+1  A: 

Since namespaces are arbitrary - they can't really be a security boundary. Anyone can create an assembly that reuses your namespace Test.

The best you can do is limit by assembly using the already mentioned internal (and InternalsVisibleTo if needed).

Edit: InternalsVisibleTo allows other assemblies to access internal methods and classes as if they were in the same assembly. You can strong name the other assemblies, which provides security. This is commonly used for test assemblies to test internal members of the main assembly, without bloating the main assembly by including the test code.

Note that, the VB.NET compiler does not respect InternalsVisibleTo - so a VB assembly cannot call into an InternalsVisibleTo attributed assembly. However, C# can call into a VB.NET assembly that has the appropiate InternalsVisibleTo attribute.

Mark Brackett
Can you elaborate on the "InternalsVisibleTo" thing?
roosteronacid