Reputation: 4806
Is it possible to prevent someone from using Reflection to get the value of an internal class variable?
I have an internal (Friend in VB) class level variable that is only supposed to be accessible when the caller knows a password. There's a method which exposes this variable which takes a password, but it is possible to access the variable directly using Reflection. Can I prevent this from happening?
Further details, code that might be hacking this variable is loaded as a plug-in to the master application.
Upvotes: 1
Views: 214
Reputation: 50286
You can make reflection harder (but not impossible) by obfuscating the source code. This mostly changes the names used in the code into illegible names that are not easy to guess. However, it provides no guarantees and any determined programmer can find out those names and reflect all he/she wants.
Or you can make reflection impossible by implementing that part of the code (the whole class) in a non-.NET language, such as unmanaged C++. For example, in mscorlib
the InternalGetHashCode
method is not implemented in managed code, so it is impossible to reflect into it to see how it works.
You can use PInvoke to call your unmanaged functionality from managed code.
Upvotes: 3