2

Is there a way I can limit the access of a class or a class object to certain functionality (e.g. writing to the console)?

I currently have a main console project which writes and reads from the console. It also loads various .dll library files (like mini programs) and uses their methods.

I was wondering if there's a way to make those .dll assemblies ask for permission or to restrict their direct access to the console's input/output and make it through e.g. a class mediator/manager.

Say someone is writing an extension (mini program) for my project. I wouldn't want it freely writing to my console application.

P.S. My question doesn't necessarily apply for Console applications only.

Richard
  • 106,783
  • 21
  • 203
  • 265
Bonnev
  • 947
  • 2
  • 9
  • 29
  • If those DLLs internally call `Console`, then someone is rather off. I think the best you can do is have a well defined specification for do's and don't for implementing a .dll for your framework. – Yuval Itzchakov Jul 13 '15 at 12:59
  • 2
    Specifically to console, you can [do that](http://stackoverflow.com/q/1412288/1997232). – Sinatr Jul 13 '15 at 13:00
  • @YuvalItzchakov I want to void their Console.WriteLine if they shouldn't have privileges to use it. They will know they shouldn't, but you know them hackers :D – Bonnev Jul 13 '15 at 15:28
  • @Sinatr This works, though the assembly could always redirect the output back with OpenStandardOutput()... – Bonnev Jul 13 '15 at 15:55

1 Answers1

0

I would suggest you to write your own CustomConsole to avoid anyone from using it. Since the original Console is standard and built in the OS itself the only way to get control over it is what Sinatr told you. Then again they can open it. If you work with your very own console you wont have to bother of who writes in it or not. After you could always pipe the standard console towards your "manager" in order to filter the message and keep those you want and remove the others.

As for the original question. I dont know any way possible to prevent a class to use methods from other classes. This would simply make the external code possibly unstable. I strongly suggest you to use some tracing tools and scan the calls that the external library does in order to know what they use (in the case that they should absolutly not call specific methods). If the external code doesen't fit with your program avoid using it.

Daneau
  • 1,085
  • 9
  • 15