Chapter 11: .NET Reflection


Years when I raked the ports wi’ pride to fill my cup o’ wrong-
Judge not, O Lord, my steps aside at Gay Street in Hong-Kong!
Blot out the wastrel hours of mine in sin when I abode -
Jane Harrigan’s an’ Number Nine, The Reddick an’ Grant Road!
An’ waur than all—my crownin’ sin—rank blasphemy an’ wild.
I was not four and twenty then—Ye wadna judge a child?

—Rudyard Kipling, writing on foolish design decisions made in previous
versions (even though they seemed logical at the time), “McAndrew’s
Hymn,” 1894.

Problem Background

So far in this book, we’ve dealt with an object’s contents (its methods, properties, events, and so on) at design time and depended on intelligent development tools to tell us what those contents were. But we haven’t yet discussed the problem of how the development tools discover these contents to present them to us. We know that the common language runtime somehow manages to read information such as the version of an assembly to provide us with useful behavior. But how does the runtime discover that information, and how can our programs do the same thing?

Our programs need the capability of discovering the contents of .NET objects at run time.

All the code that we’ve seen so far in this book has been written with the assumption that a programmer knows at design time exactly which objects she wants to create and which methods she wants to call on them. It’s easy to write and efficient to run, as long as you know the object classes and methods in advance. To create an object, she simply writes MyObject = new ThisClassOfObject and calls a method by writing MyObject.SomeMethod ( ). The compiler then translates this source code into intermediate language (IL). This process is known as early binding because the connection between the source code and the object class and method is made at compile time.

It’s easy to create objects and call methods that we know about at compile time.

But there are large and important classes of problems in which the use of early binding is not possible, in which the client can’t possibly know which class of object to create or which method to call on it until run time. For example, consider the Web service infrastructure that I discussed in Chapter 4. The .asmx page that you write contains the name of the class that handles incoming Web service calls, and the SOAP packet contains the name of the method to call and the parameters to pass it. Somehow the Web service infrastructure has to create an object of the class named by a string in the .asmx file and call on it the method named by a string in the SOAP packet. This process is called late binding because we don’t find out about the object class and method names until just before making the call. Late binding is also used in many other applications, such as scripting. Our programs need to be able to create objects and access their contents from information they discover at run time.

But we don’t always know that, so we also need to create and invoke methods on objects that we learn about at run time.




Introducing Microsoft. NET
Introducing Microsoft .NET (Pro-Developer)
ISBN: 0735619182
EAN: 2147483647
Year: 2003
Pages: 110

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