You are right that reflection can be contrasted with any number of good design principles, but it can also be an important building block that you can use to support good design principles - for example, software that can be extended with plugins, invert controls, etc.
If you are concerned that it is an ability that should be discouraged, you might have a point. But it is not so convenient to use as true language functions, so it is easier to do something right.
If you think reflection should be impossible, you are dreaming!
In C ++, there is no reflection as such. But there is a basic “object model” that computer code created by the compiler uses to access the structure of objects (and virtual functions). That way, a C ++ programmer can break encapsulation in the same way.
class RealClass { private: int m_secret; }; class FakeClass { public: int m_notSecret; };
We can take a pointer to an object of type RealClass and simply pass it to FakeClass and access the "private" element.
Any limited system must be implemented on top of a more flexible system, so you can always get around it. If reflection was not presented as a BCL function, someone could add it using a library using unsafe code.
In some languages, there are ways to encapsulate data so that it is not possible to receive data other than certain specific methods. But you can always be fooled if you can find a way to get away from your tongue. An extreme example would be scope variables in JavaScript:
(function() { var x = 5; myGetter = function() { return x; }; mySetter = function(v) { x = v; }; })();
After that, the global namespace contains two functions: myGetter and mySetter , which are the only way to access the value of x . Javascript has no reflexive ability to get x any other way. But it must run in some kind of host interpreter (for example, in the browser), and therefore, of course, there is some terrible way to manipulate x . A memory error in the plugin could have done this by accident!