I just realized this morning a lot of my articles talk about how these new code constructs replace the majority of the manual reflection we used to have to do, when I haven’t really talked about Reflection itself! So here’s a quick summary.
Reflection is the ability of a programming language to look at it’s own compiled code programmatically. Using the information you can derive from a compiled library, you can instantiate and execute code from that library. This is mostly done using the Type and Assembly classes. Most people would think, “Well, if you have the library already, why don’t you just reference it an use it that way?” Yeah, I know, it doesn’t sound entirely useful right off the bat, but there’s a number of scenarios where it does come in handy. I’ll list a few examples that we use in our own code.
Our internal library uses reflection to instantiate services running locally in debug mode. Here’s a summary (names changed to protect the innocent)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
On the other side of the fence, we use attributes to generate service endpoints
1 2 3 4 5 6 7 8
DevBoard is an internal dashboard application that uses reflection to go through each assembly loaded in a specific folder, looking for classes that implement IChannel or ITicker (interfaces I defined). It then uses that list to dynamically instantiate and run the Watch/Update method on each class, which generates the content that we see on the screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
The way iQcommerce populates clients’ web pages with data uses Reflection. We parse the HTML, looking for specific attributes that specify classes in our Item Library Assembly, and then use those classes to generate the HTML that is displayed to a customer.
1 2 3 4 5 6 7 8 9 10 11
We use Reflection in our CacheProxy class to determine if a method the application is calling should have its results cached. This is done by checking if the method has a CacheAttribute defined on the interface’s method definition.
1 2 3 4 5 6 7 8 9 10 11
So, in summary, you can see a number of situations where Reflection is useful, and it’s a powerful feature of any current programming language. New constructs in .NET 4.0 like the “dynamic” keyboard (or any of the new Dynamic Language Runtime for that matter) wrap this already-existing functionality to make it even easier to use Reflection in your programming tasks.