Chapter 8 Upon Reflection

Team Fly 

Page 191

Chapter 8
Upon Reflection

REFLECTION IS ONE OF those new technologies that is described in various different ways by various experts (rather like Web services). It's new, at least, to Visual Basic programmers.

And we cannot pretend that reflection isn't just a little bizarre. Like recursion, it can involve a kind of self-consumption—an esoteric process. Something about reflection isn't quite normal.

At its most elemental level, reflection means finding out details about the contents of assemblies during runtime. And, like much in .NET, reflection has its antecedents in the C language, specifically the Runtime Type Information (RTTI) feature of C++.

Reflection permits you to learn the type, and members, of an object, while a program is running. But there's more: After you've discovered this information about objects, you can then do something with your knowledge. You can use reflection to execute the discovered methods, access discovered properties, pass parameters, and even generate, compile, and execute new code during runtime.

What Use Is It?

What good is all that? Some cool tricks become possible. For example, with reflection you can write code that will later (during runtime) consume objects that have not yet been designed. Or you can defer making the choice of which methods to invoke until runtime.

Sometimes you don't know the context or environment that will be in effect during runtime. One way to deal with this problem is to use reflection, thereby permitting your code to select an appropriate method during runtime. Think of this use of reflection as an advanced form of Select...Case.

Reflection can also be used to build custom object browsers, code–generators, sophisticated self-commenting code, utilities that examine and secure compiled executables, and advanced, dynamic debugging tools that facilitate runtime error trapping.

Understanding Types

The target of reflection is usually an entire assembly. The term assembly is new in VB.NET. It's what was traditionally called an application—a collection of related ''types" and resources that do a particular job, such as word processing.

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

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