Does reflection breaks the idea of private methods, because private methods can be access outside of the class?

前端 未结 14 1094
甜味超标
甜味超标 2020-11-28 08:06

Does reflection break the idea of private methods? Because private methods can be accessed from outside of the class? (Maybe I don\'t understand the meaning of reflection or

14条回答
  •  醉酒成梦
    2020-11-28 08:42

    Yes, it does break encapsulation. But there are many good reasons to use it in some situations.

    For example:

    I use MSCaptcha in some websites, but it renders a < div> around the < img > tag that messes with my HTML. Then i can use a standard < img> tag and use reflection to get the value of the captcha's image id to construct a URL.

    The image id is a private Property but using reflection i can get that value.

提交回复
热议问题