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-
It's not so much modifying code at execution time, but examining objects and asking them to execute code without knowing their type statically.
One simple way of describing it would be "a somewhat painful way of making a statically typed language behave dynamically."
EDIT: Uses:
SubmitButton_Click
and ASP.NET will attach that method as a handler for the SubmitButton
's Click
event (if you have autowiring turned on)Is it a good idea? Well, only when the alternatives are painful. I prefer static typing when it doesn't get in the way - then you get lots of compile-time goodness, and it's faster too. But when you do need it, reflection lets you do various things which otherwise just wouldn't be possible.
In Java it is basically a way to instantiate a class without knowing about it before hand. Say you want the user to be able to change a configuration file by adding the class they want your program to use (say you have numerous implementations of some interface). With reflection you can create an object based on just it's name, method signature, etc. . . and then cast it to your interface.
The first good example I can think of off the top of my head is for when you need to execute a set of methods on a given object without knowing at compile-time what methods will exist in it.
Take unit testing frameworks for example. The test runner class that is responsible for running all your unit tests doesn't know ahead of time what you are going to name your methods. All it knows is that they will be prefixed with "test" (or in Java 5's case, annotated with @Test
). So when given a test class, it reflects on that class in order to get a list of all the methods in it. Then, it iterates through those method names as strings and calls those methods on the object if they start with "test". That wouldn't be possible without reflection. And that's just one example.
Reflection is a facility where you can query an object about its attributes at runtime. For example, Python, Java and .Net have facilities where you can find the instance variables or methods of an object.
An example of an application for reflection is an O/R mapping layer. Some use reflection to construct an object by quering its properties at runtime and dynamically populating an instance. This allows you to do this programatically based on metadata from some sort of data dictionary without having to recompile the application.
To take a simple example, I'll use Python because its reflection facilities are very simple to use and involve less boilerplate than those of java or .Net.
ActivePython 2.5.2.2 (ActiveState Software Inc.) based on
Python 2.5.2 (r252:60911, Mar 27 2008, 17:57:18) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class foo:
... def __init__(self):
... self.x = 1
...
>>> xx = foo() # Creates an object and runs the constructor
>>> xx.__dict__ # System metadata about the object
{'x': 1}
>>> a = xx.__dict__ # Now we manipulate the object through
>>> a['y'] = 2 # its metadata ...
>>> print xx.y # ... and suddenly it has a new instance variable
2
>>>
Now, we've used basic reflection to examine the instance variables of an arbitrary object. The special variable __dict__
on Python is a system property of an object that has a hash table of its members keyed by the variable (or method) name. We have reflectively examined the object and used the reflection facilities to artificially poke a second instance variable into it, which we can then display by invoking it as an instance variable.
Note that this particular trick doesn't work on Java or .Net, as the instance variables are fixed. The type system of these languages doesn't allow new instance variables to be added at runtime in the way that python's 'duck' typing system does. However, you could have reflectively updated the value of an instance variable that was declared in the type definition.
You can also use reflection to dynamically construct method invocations and perform various other neat tricks such as instantiating an object based on a parameter. For example, if you had some sort of plugin based system where certain capabilities were optional, you could use reflection to query the plugin about what services it offered (perhaps by querying whether certain interfaces were implemented) without requiring explicit metadata.
Many dynamic language interfaces such as OLE automation use reflection as an integral part of the interface.
Assemblies contain modules, modules contain types, and types contain members. Reflection provides objects that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can then invoke the type's methods or access its fields and properties. Typical uses of reflection include the following: