views:

74

answers:

4

I'd like to declare some members of a module accessible to an entire namespace, but have them not be accessible from without.

Is this possible?

Thanks.

+5  A: 

No, there is only the internal accessibility that limits access to your current Assembly.

Access Levels in C#

Since your post has the VB.NET tag: internal is a c# keyword, it's equivalent in VB is friend:

Friend Keyword in VB.NET

With the hole list being:

Access Levels in VB.NET

Tigraine
+9  A: 

(apologies for the C# terminology/examples...)

No. You can restrict :

  • to the current assembly (internal)
  • to named other assemblies ([InternalsVisibleTo])
  • to subclasses (protected)

(and some combinations, such as protected internal)

That's about it. Nothing based on the caller's namespace. After all - I can just do this:

namespace Your.Namespace {
    public static class MyEvilClass {
       public static void DoEvil() {
           YourPrivateClass.PushTheRedButton();
       }
    }
}

and I've broken it...

Marc Gravell
+2  A: 

While there is no compile time support for this you can certainly enforce it at runtime.

 new StackTrace().GetFrames()

This will give you the stack trace that you can check to see if the call came from an allowed namespace.

PLEASE DO NOT EVER DO THIS THOUGH. It will certainly make other peoples lives worse.

Derek Ekins
A: 

there is no concept of namespace once your code is compiled to IL. What the compiler see is just a string with dots in the type's name. If you want to restrict the caller to known parties, check the calling assembly's public key and compare to a white list.

Sheng Jiang 蒋晟