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

前端 未结 4 1361
北恋
北恋 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 05:53

    The quick answer will be: No this isn't possible, and if you need to do it, you're Doing It Wrong.

    How would this even work? Does it depend who who's running the code or who wrote it?

    Edit There's kind of a way using InternalsVisibleTo and restricting accessing in source control to the assemblies that InternalsVisibleTo is specified for. See Jordão's answer

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-15 06:00

    Can you try using Extensible C# developed by ResolveCorp, some of the links for study and implementation are:

    http://zef.me/782/extensible-c

    http://www.codeproject.com/KB/architecture/DbCwithXCSharp.aspx

    http://weblogs.asp.net/nunitaddin/archive/2003/02/14/2412.aspx

    http://www.devx.com/dotnet/Article/11579/0/page/5

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题