Chapter 10: Using Reflection


Overview

Reflection is the ability of code to examine itself. The fact that code has the ability to examine itself should come as no surprise because this ability is needed for even such mundane tasks as figuring out the particular address of a method that needs to be called. But Microsoft has taken the mundane and made it spectacular. Microsoft has given you the ability to examine code. The fact that code has knowledge of itself is nothing new, but just because the code knew about itself did not mean you could get that information. And then Microsoft introduced one more awesome ability: You can create custom attributes with which to tag your code, and you can read these attributes using reflection. This is something unique to the .NET Framework.

Note

To be fair, Java has had reflection since its inception, but Java does not give the developer the ability to create custom attributes. So you can examine the code all you want to determine things about it, but you cannot say anything about it.

In this chapter you will examine reflection in the context of two practical examples. The first example demonstrates reading from attributes to determine how to load a listview without knowing anything about the object that you are taking data from and without knowing anything about the data itself. This project is a small, independent demonstration. For the second example you will incorporate attribute classes into your application in the RegionDescription class. This example shows you how to turn the business rules that you have created into custom attributes and make the class truly self-aware.

Tip

After developing this method of implementing business rules, my team and I were able to save approximately 30,000 lines of code in a recent project. We were able to quantify this by extrapolating out the amount of code we saved after converting just a few classes to this method. This also made maintenance of the application much simpler!

start sidebar
Generating Code Dynamically

One other ability of reflection that I wanted to mention (but that I will not cover in this book) is the ability to dynamically create code. The namespace that contains the classes necessary to do this is the Reflection.Emit namespace. Dynamic generation of code can get very complicated, so you should be careful about using this ability, but you can do some incredible things with it. Imagine if you have an application that performs complex calculations, but you do not necessarily know what all of those calculations are beforehand. Say you have given the users the ability to create calculations later and specify how the application processes the calculation. To do this you might take a calculation and dynamically generate the code needed to process the calculation, and then you gain the ability for an application to be expanded without any additional coding by a developer!

I do not expect this ability to catch on overnight because it is a highly complex area of development. To develop code using Reflection.Emit, you need to know a great deal about the Microsoft Intermediate Language (MSIL). An excellent book on the subject is Compiling for the .NET Common Language Runtime by John Gough (Prentice Hall, 2001).

end sidebar




Building Client/Server Applications with VB. NET(c) An Example-Driven Approach
Building Client/Server Applications Under VB .NET: An Example-Driven Approach
ISBN: 1590590708
EAN: 2147483647
Year: 2005
Pages: 148
Authors: Jeff Levinson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net