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

前端 未结 14 1054
甜味超标
甜味超标 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:35

    Reflection makes it possible for any CLR class to examine and manipulate properties and fields of other CLR classes, but not necessarily to do so sensibly. It's possible for a class to obscure the meaning of properties and fields or protect them against tampering by having them depend in non-obvious fashion upon each other, static fields, underlying OS info, etc.

    For example, a class could keep in some variable an encrypted version of the OS handle for its main window. Using reflection, another class could see that variable, but without knowing the encryption method it could not identify the window to which it belonged or make the variable refer to another window.

    I've seen classes that claim to act as "universal serializers"; they can be very useful if applied to something like a data-storage-container class which is missing a "serializable" attribute but is otherwise entirely straightforward. They will produce gobbledygook if applied to any class whose creator has endeavored to obscure things.

提交回复
热议问题