I prepared some C# dll for my customer that doing some functionality.
The thing is that I use also same dll.
How can I make some methods available to him and all methods available for me.
Thanks,
问题:
回答1:
Use a shared code base
Simply compile two projects. One contains the source for the DLL you are providing to the customer, and the other contains all the source, which you keep for yourself.
- Advantage: They see none of the source you want to keep for yourself, and you don't have to set up any sort of special hosting - just send them the DLL
- Disadvantage: You have to do extra work to set up your code base to be fork-able. It might be a large development investment to do this sort of refactor.
Provide a web service
Provide a web service for the customer to access the code that they are allowed to access.
- Advantage: They see none of the source at all.
- Disadvantage: Depending on the code, it might be a lot of work (to get security up-to-snuff), and might be impossible (to get performance up to snuff). Or it might be easy, because maybe these aren't big concerns. You also have to set up dedicated hosting for this to work.
Use the InternalsVisibleTo
attribute in the shared DLL
- Mark those methods you want to use
internal
- Code sign your own code that uses the DLL
- Code sign the shared DLL
- Add
[assembly: InternalsVisibleTo]
to the shared DLL
See: http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx
- Advantage: This is a very easy solution to code up
- Disadvantage: The client will have access to the full source code, via Reflector. You could mitigate this by obfuscating your code, or setting up a licensing agreement that legally forbids them to reverse engineer your code.
回答2:
You have to compile two versions of your assembly. The version you distribute to your cutomer needs to completely exclude the methods you don't want them to access, otherwise they'll be accessible through Reflection regardless of how you "hide" them.
(Assuming you don't want your assemblies to be signed)
回答3:
You could use the C# preprocessor directives like #define
to compile two versions of the library from the same code base.
Then ship one version to your customer, keep the other version for yourself.