What exactly is Reflection? I read the Wikipedia article on this subject and I understand that it is a kind of meta-programming, where the program can modify itself at run-
Really, reflection should be thought of as sort of an amplifier for code. It can make good code better and cleaner, and bad code worse. What does it do? It really allows you to write code, that your not totally certain what it's going to do at the time you write it. You have a general idea, but it allows you to not code what objects, methods, and properties are going execute when the program is compiled.
Other posts are correct when they say it allows the program to execute code based on configuration values, but it's real power is that it allows you to severely bend the rules of object oriented programming. That's really what it does. It's kind of like turning the safety measures off. Private methods and properties can be accessed through reflection along with just about anything else.
An excellent example of when MS uses reflection is with data binding on data object. You specify the name of the text field and data field for an object to bind to a drop down list etc., and the code reflects the object and pulls out the appropriate information. The data binding object does the same process over and over again, but it doesn't know what type of object it has to bind with. Reflection is a convenient way of writing a little bit of code to handle all the possible cases.
Reflection is (basically) the ability of a program to query for type information that was available to the compiler. So, for example, given the name of a type you can query for the methods it contains. Then for each method you can query for the types of the parameters they take etc etc etc.
It's useful for runtime configuration where you've got a config file that specifies the behavior of your application. The config may contain the names of concrete types that you should use (as is often the case with IOC containers). Using reflection you can create an instance of this concrete type (via a reflection API) and use it.
I'll give you an example.
As a programming exercise I wrote an mp3 file checker. It scans my music library and displays the id1/id2 tags I'm interested in in a DataGridView. I use reflection to get the properties from the mp3 info class without the UI code having to know anything about that class. If I want to change what information gets displayed I can either edit the mp3 info class or change its configuration (depending on how I wrote the class) and don't have to update the UI.
It's also meant that I've been able to use Dependency Injection to use the same from end to display information about digital photographs just by swapping the data library class.
Reflection has been useful to me in at least 1 project I can think of. We wrote an internal "Process Manager" program that performs a lot of key business processes at certain intervals. The project is set up so that the core is really just a windows service with timer objects that fire off every 30 seconds or so and check for work to do.
The actual work is being done in a class library (appropriately called "WorkerLib"). We define classes with public methods that perform certain tasks (moving files around, uploading data to remote sites, etc.) The idea is that the core service can call methods from the worker library without knowing anything about the methods it's calling. This allows us to create a schedule in the database for jobs, and even add new methods into the class library without ever having to alter the core system.
The basic idea is that we can use reflection in the core service to execute methods whose names we have stored in the database defining the schedules. It's pretty neat in practice. Our core service is solid and handles executing the jobs as needed, while the actual worker library can be expanded and changed as necessary without the core even caring.
If you need further explanation, please feel free to ask. This was simply the best way I could think of to explain a real-world scenario where reflection really makes things easier for us.
Another example: I have code that I use that takes the output of a database - which is a set of rows with named columns - and feeds it into an array of objects. I iterate through the rows, and if the target object has a property with the same name and type, I set it. This makes for my data-getting code just looking something like this:
SqlDataReader sdr = Helper.GetReader("GetClientByID", ClientID);
Client c = new Client();
FillObject(sdr, c);
return c;
Reflection is useful for runtime configuration, allowing parts of a system to be driven through external configuration.
For example, a class factory could construct different concrete types based on an input file, where the concrete types require different configuration information to call a concrete constructor rather than using a builder interface. (The constructor method of the object being located using reflection).