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.




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