In C#, how to restrict who can call a method at compile time

前端 未结 4 1360
北恋
北恋 2021-01-15 05:25

In C#, is it possible to restrict who can call a method at compile time?

I\'ve looked into directives, but that didn\'t work since I can\'t assign values to symbols.

4条回答
  •  囚心锁ツ
    2021-01-15 06:07

    The requirement is to restrict access to a method at compile time for specific developers given the method exists in a pre-compiled assembly.

    One way is to mark the method private or internal, it won't be callable by anyone outside the assembly. UPDATE: Also take a look at the InternalsVisibleTo attribute, which is used to define which assemblies can "see" internals of your assembly.

    Another way is to divide the code you want to distribute from the code you don't want people to call into separate assemblies. Maybe you just share an assembly mostly of interfaces with your users, that they them compile against; and you have a separate assembly with implementations that they shouldn't reference directly. Your internal team would have access to the implementation assembly. This is just a common form of dependency management, the dependency inversion principle.

提交回复
热议问题