I have 2 projects:
- A Class Library that contains m开发者_JAVA百科y classes/business logic
- A WPF application that uses the Class Library (DLL)
I have been given the task of hiding all of the Class Library functions and classes so that it cannot be called or used by external applications.
My problem is that the WPF application is an "external application".
I changed all Public scope modifiers for all of the classes and properties to Friend.
I then tried combining both projects into one solution...which of course didn't work. Within the solution that contains both projects, I also tried changing the "Assembly" of the Class Library to match the WPF (Because "Friend" allows code within the same assembly to use it)...but this doesn't work either.
Does anyone know of a way to accomplish this?
Thanks
-Frinny
If they are different assemblies, then there is no direct way to do that. You can get a bit cheeky with InternalsVisibleToAttribute
, but that is a bit hacky.
Frankly, you can't stop external code calling your methods unless you go to extremes. A malicious app can just walk past any modifiers with reflection if they want (since the app calling you probably owns the AppDomain, it can assert full-trust). There is some support for caller-based security / permissions, but the regular stuff is largely disabled by "full trust" - you would need to check the caller yourself. Not worth it, IMO. Especially since from 4.0 code-access permissions are not used any more (see MSDN)
Use the InternalsVisibleTo attribute.
you should be able to set the InternalsVisibleTo attribute so that the internal classes are visible to your WPF application, and this will allow it to still se them but other apps will not be able to.
Some help with it here
As has been mentioned, this will work for only the most simple of situations. People will still be able to call your methods using reflection if they want.
精彩评论