Erick Stone
Erick Stone

Reputation: 809

Ensure only one class can access a reference dll?

I have a project where I want only one class to have access to a dll. I would be content with hiding the dll from intellisense, but still having access to it if that is an option. Either way, I want only one class to use the dll and for it not to be seen and/or accessible otherwise. I'm using C# in visual studios.

Upvotes: 0

Views: 407

Answers (2)

forsvarir
forsvarir

Reputation: 10839

An alternate approach, if you goal is to stop people using the functionality of the dll by accident is to push your wrapper class into an intermediary assembly. Your project then references that intermediary project, rather than the underlying dll which effectively hides it. So your project structure would change to something like this:

Main Project -> references API Wrapper Project -> references API DLL

For this to work, your wrapper project needs to make sure that it doesn't accidentally expose any of the API DLL classes through its public interface.

Obviously this doesn't stop your developers from going in and adding a reference to the API DLL so that they can use the library directly, but if the goal is to stop accidental access to the API DLL classes because intellisense has helped the developer out a bit too much then it might be a viable option.

Upvotes: 0

Erre Efe
Erre Efe

Reputation: 15557

Simply said: You can't do that (but keep reading).

Basically, a DLL (From the .NET perspective) is a bunch of code and config files. No more than that. So, given that you'll need to make public those classes in order to be used from another ones outside that assembly then you can not.

What you can do (I ended up doing this a few years ago) is to use some kind of reflection to determine which class (and namespace) is trying to access your assembly and only permit the proper one.

Other way is to implement a key negotiation between your DLL and the permitted assembly. You'll need to implement those manually, as far as I know.

Anyway, keep in mind there's always a way to bypass this kind of protection by disassembling and modifying your code. So, at least, don't forget to obfuscate the file. Anyway, this will just make it a little more painful, but still possible.

Upvotes: 1

Related Questions