Free Programming Books
Free download ebooks on computer and programming

Free Ebook "Building Client/Server Applications with VB .NET: An Example-Driven Approach" Sample Chapter

Building Client/Server Apps..
Free downloads Chapter 10: Using Reflection.
Download chapter

Building Client/Server Applications with VB .NET: An Example-Driven Approach is based on Release 1.0 of the .NET Framework / Release 1.0 of Visual Studio .NET + .NET Framework Service Pack 1. Jeff Levinson walks readers through how to write a complete application- no "snippets" of code- and will show readers examples of how, when, and why to perform a task. Building Client/Server Applications with VB .NET: An Example-Driven Approach will be the manual on software development for Enterprise application development.

< < prev next > >

Using Reflection

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!

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).

Understanding Attribute Classes

The root of all reflection is the System.Attribute class. An attribute class provides information about a coding construct. So what is an attribute class? An attribute class is a class you can create and, for lack of a better description, "attach" to anything. You could attach them to a class, property, method, structure, enum, and so on. You can designate properties that only allow an attribute class to be attached to certain types of code structures or to everything-it is completely up to you. And how does this help you? It allows you to describe, or give additional properties to, a specific piece of code. Using reflection, you can examine these classes to learn information about your code elements.

NOTE Attribute classes are passive. That is, they are compiled into the code, and they cannot react to changes in data-they can only examine the data after the fact. So, if you need to stop a property from being changed unless it follows certain rules (as opposed to changing the value and marking it as a broken rule), you need to use a combination of attribute classes and business rule checking as shown in earlier chapters.

Used properly, reflection can make classes more flexible and more reusable. It can also give you the ability to dynamically generate information based on your classes. The example you will see first demonstrates that ability. After you have worked through this example, you will probably find some creative ways to use this unique and awesome ability of the .NET Framework.