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

前端 未结 4 1365
北恋
北恋 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:00

    Draft:

    1. Compile the restricted code into (obfuscated) DLLs: TypeA.dll, TypeB.dll etc.
    2. Define an interface for each type, and compile them into separate DLLs: ITypeA.dll, ITypeB.dll etc.
    3. Create a "guard assembly", and embed all restricted assemblies into it: Guard.dll. This has a ResolveEventHandler, and methods to instantiate different types defined in the embedded restricted DLLs. Instances are returned through their interface.
    4. Developers get the interface DLLs and the Guard.dll. Each developer can get a Guard.dll with special authentication tokens in it. For example, a Guard.dll can be bound to PC, an IP address, a GUID issued to the developer, anything.
    5. The developer can instantiate those types for which she has the proper authentication code, and uses the object instance through an interface.

    Sorry this is a bit fuzzy, because it was more than a year ago when I used these techniques. I hope the main idea is clear.

提交回复
热议问题