What is the difference between self-modifying code and reflection?

683 views Asked by At

Self-modifying code is code that "alters its own instructions while it is executing". This is not typically done outside of assembly language or viruses.

Reflection is just the ability of a program to access its own namespace dynamically, in order to reference functions and classes and variables dynamically. According to this article, reflection is not just introspection (a program's ability to examine itself), but also intercession (a program's ability to modify itself).

So, is the difference that reflection refers to a mild form of self-modifying code where only the variable/class/function name gets "modified" within the instructions? That is, reflection is a milder, less "dramatic" form of modification compared with the ability to modify the nature of the entire instruction itself as in self-modifying code.

Do I have this distinction right?

1

There are 1 answers

0
Preet Sangha On BEST ANSWER

No, one is about changing the code during execution. The other about reading the structure and metadata (introspection) of the code during execution.

They can be mutually exclusive. You don't need to know how the code is to modify it (if the OS allows you that is).

Typically you can use reflection to execute code in a non 'normal use case' manner, but it's still the same code. contrast this to modifying the code.

The goals are completely non aligned.

However I suppose one example where they intersect in a small way is to consider a function (F) that calls two other functions - A then B. You could reflect that knowledge and then call B then A (thus modifying the use case of (F)). as you can see it's not not modifying code, rather just the intended use case.