Possible Duplicates:
When do you use reflection? Patterns/anti-patterns
What exactly is Reflection and when is it a good approach?
What is the need for reflection in .NET? What are the situations where it comes in handy?
Possible Duplicates:
When do you use reflection? Patterns/anti-patterns
What exactly is Reflection and when is it a good approach?
What is the need for reflection in .NET? What are the situations where it comes in handy?
I think the Wikipedia article on reflection is pretty good:
In computer science, reflection is the process by which a computer program can observe and modify its own structure and behavior. The programming paradigm driven by reflection is called reflective programming. It is a particular kind of metaprogramming.
In many computer architectures, program instructions are stored as data - hence the distinction between instruction and data is merely a matter of how the information is treated by the computer and programming language. Normally, 'instructions' are 'executed' and 'data' is 'processed'; however, in some languages, programs can also treat instructions as data and therefore make reflective modifications. Reflection is most commonly used in high-level virtual machine programming languages like Smalltalk and scripting languages, and less commonly used in manifestly typed and/or statically typed programming languages such as Java and C.
Reflection allows you to get information about the type of an object at execution time. This can be helpful in many situations including (but limited to) serialization and object-relational mappings.
Reflection is used when you don't know at compile time which time your object is or what action to do or on what property...
You can use reflection to find all object of Type X in a given Assembly, or invoke some method by it's name (fetched from a db, or config file...), same thing for setting a property.... things like that (these are the case where we used it)
Let's say you're writing a basic serialization routine, that will serialize any object to XML. How would you make it generic enough, so that it can work for any object? If you have a class where you know all the properties, then you can easily write a "ToXml()" function, where you manually write out all the properties to XML. What if you want to extend this though to ANY object? In that case, you need to reflect over the properties at runtime, and write them out to the XML.
There are many more uses for it, that's the first one that came to mind.
There are many uses for reflection. The .NET Framework uses it for serialization and for data binding, it can also be used for creating tools that examine your code like Reflector, FxCop and NUnit as well as ORM database frameworks. It has a wide variety of uses at runtime from logging specific things about an object to Dependency Injection Frameworks. It can also be used to dynamically execute methods or set properties at runtime as is done with custom attributes. It can also be used in higher level programming such as metaprogramming and self-modifying code.
It's used for metaprogramming, which is when you're coding about code itself rather than the usual business problems
It can also be very useful when testing your application, e.g. unit tests or other types of test frameworks. By using reflection you could for example create an xml file (or some other input data file in suitable format) and have your program analyze it and call methods that are defined in the file. This is how Fitnesse work.