.NET Can a member of a module be declared accessible only for the namespace?

I would like to declare some members of the module available to the entire namespace, but so that they are not accessible externally.

Is it possible?

Thanks.

+3
source share
4 answers

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

No. You can limit:

  • to current build ( internal)
  • to indicate other assemblies ( [InternalsVisibleTo])
  • for subclasses ( protected)

(and some combinations like protected internal)

What about that. Nothing depends on the caller's namespace. In the end, I can just do this:

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

and I broke it ...

+9

, , .

#

VB.NET: internal - #, VB :

VB.NET

:

VB.NET

+5

, , .

 new StackTrace().GetFrames()

, , , .

, - . , , .

+2

There is no namespace concept after your code has been compiled into IL. What the compiler sees is just a string with dots in the type name. If you want to restrict the caller to known parties, check the public key of the calling assembly and compare with the white list.

0
source

Source: https://habr.com/ru/post/1721740/


All Articles