Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do access modifiers affect reflection also?

I always believe they did, but seeing some answers here make me doubt...

Can I access private fields/properties/methods from outside a class through reflection?

like image 953
juan Avatar asked Sep 18 '08 19:09

juan


2 Answers

Yes you can access private fields via reflection. This is how a lot of ORMs go about populating an object without going through your properties (which will invoke business logic you might not have intended to be run on an object load).

Access modifiers are not a form of security!

like image 110
Ryan Rinaldi Avatar answered Nov 24 '22 00:11

Ryan Rinaldi


You do, however, need extra permissions for accessing private/protected/internal fields/properties/methods from outside a class through reflection.

like image 20
Tobi Avatar answered Nov 23 '22 22:11

Tobi