What is Reflection?

I knew that PowerShell constructs such [bool] – which defines the boolean data type – were called accelerators, but I never knew why and didn’t bother much as I figured I’d learn more about them soon or later.

But yesterday while reading Bruce Payette’s “Windows PowerShell in Action” that book too mentioned the word accelerator without defining it, and so I started Googling a bit about it. This Googling introduced me to accelerators for WMI – very useful, something I must explore sometime – and that got me curious on how to find all the available accelerators in PowerShell. This in turn bought me to the topic of reflection as that’s what one uses to get a list of accelerators in PowerShell.

So what is reflection? Truth to be told it still does not make much sense to be but what I understand is that reflection is the act (or ability) of an object to look at itself (see it’s “reflection” in the mirror so to say) and learn what it’s capable of – what methods it has, what are it’s properties, and so on. And it is useful because sometimes you may encounter objects that you are getting from elsewhere, and reflection is what you need to learn more about the object.

I guess an example will make it clearer.

Say I define an object called $date as below:

As the creator of this object I know it’s of type [datetime]. And since PowerShell provides a cmdlet Get-Members I know I can use it to examine the methods and properties of this object.

But what if I wasn’t the creator of this object. All I know is that I have a script which will get some object as input and I am supposed to examine it and take further action depending on what sort of an object it is. Further, assume the Get-Member cmdlet isn’t available, or for some reason you can’t use it on the object you are receiving (or maybe Get-Member itself depends on reflection, which we’ll talk about below, so it won’t work unless the concept of reflection is supported by PowerShell). Either ways – you don’t have Get-Member available, period.

In such a situation how would you know what is contained inside the object? That’s where reflection comes into the picture.

Before we go further, it’s worth reading this WikiPedia page on reflection, and this and this StackOverflow post. All these links explain reflection, but the takeaway from the Wikipedia page are the examples it gives of reflection in various languages, and the takeaway from one of the answers in the second StackOverflow post is that all objects in Java have a getClass method which lets one reflectively examine an unknown object.

I created a custom PowerShell object to see if PowerShell objects have a similar method.

Sure enough, GetType() looks like what we are after. (GetHashcode() and ToString return an integer and string respectively (as seen from their definitions) so we can straightaway eliminate them).

Let’s dig deeper:

The GetType() method returns an object of type RuntimeType which contains many methods and properties. The interesting bits for us are GetProperties, GetMethods, and so on. Let’s explore these:

Now let’s try this on our “unknown” $date object:

As you can see, without knowing anything about the object we have managed to identify its type, the methods & properties it contains, and even invoke one of the methods. That’s reflection!

Now I have to figure how to use this info to get a list of accelerators in PowerShell. Which is the topic for another post, of course …