Reflection
Reflection looks a bit like a delegate in places, but the two are pretty different. The point of a delegate is compile-time safety. Reflection gives up the compile-time checks and gets runtime flexibility in return: it calls or gets a given object by string, which lets a program read or change any code at runtime. Reflection is essentially a “meta operation”, code operating on code, which is also what “reflection” literally means.
Take the scheduling case I wrote about before: say my scheduler works by subclassing, and the subclass only cares about the concrete job. But if what I need is to figure out how many jobs inherit the base class, the current approach is to scan with reflection. Right after I first learned how delegates work, my idea was, could the base class do lifecycle management, wrap the subclass in a run, so what the scheduler actually runs is run, then have the base class declare an event, and this run is the—and this is where it hit me—the delegate property predefined in the base class gets reset every time it’s inherited and instantiated, and then the event queue is empty.
So at startup you scan the program itself to see how many subclasses inherit this base class, and only then can you hand them to the scheduling framework to register in one place. Otherwise, to know how many jobs there are you have to get the jobs first, to get the jobs you have to register them first, and to register them you have to know how many jobs there are, and in the end no job gets registered. The framework has to know the count before instantiation, so there’s no way I can wait until after that to put jobs into the event, it has to be known before startup, and the only way is reflection scanning for classes that inherit base. This is a type discovery problem, not a problem of interaction between the objects a delegate carries (a subscriber operating through a delegate the publisher exposes).
Translation note. I wrote this in Chinese. This English version is an LLM translation, so the wording is not mine even though the thinking is. Original: 反射.