asp net 3.5unleashed

Page 449

Chapter 20: CLR Hosting, AppDomains, and Reflection In this chapter, I’ll discuss three main topics that really show off the incredible value provided by the Microsoft .NET Framework. In particular, I’ll explain how many of Microsoft’s existing application and server products intend to leverage the common language runtime (CLR) in the future. You’ll see that your investment in learning the .NET Framework today will certainly pay off down the line. I’ll also talk about AppDomains, a mechanism offered by the CLR to reduce memory usage and improve system performance. And finally, I’ll discuss reflection, a mechanism that allows you to design dynamically extensible applications that your types or another party’s types can easily enhance.

Metadata: The Cornerstone of the .NET Framework By now, it should be obvious to you that metadata is the cornerstone technology of the .NET Framework development platform. Metadata describes a type’s fields along with its methods. Metadata is what allows a type developed in one programming language to be consumed by code developed in a completely different programming language. In addition, the garbage collector uses metadata to determine what objects are reachable; the metadata indicates what other objects an object can refer to. Development tools, such as Microsoft Visual Studio’s editor, use metadata to provide IntelliSense and other help-related assistance. And, of course, metadata is used to serialize and deserialize objects so that they can be persisted to disk or sent over the network. In fact, this ability to use metadata to easily serialize and deserialize objects over the wire is what makes building XML Web services with the .NET Framework child’s play. Throughout this book, I’ve been using the ILDasm.exe tool that ships with the .NET Framework SDK. This tool parses the metadata contained inside a managed module or assembly and shows the metadata information in human readable format. The act of examining metadata is called reflection; in other words, ILDasm.exe reflects over the module’s or assembly’s metadata and shows the results to the user. Reflection is an incredibly powerful tool for developers. Reflection allows developers to build dynamically extensible applications. For example, anyone can produce a type and package it in an assembly. However, if that type follows certain rules, the Visual Studio .NET Windows Forms and Web Forms designers can integrate the type (component) into the designers. Visual Studio can add the type to the Toolbox window, and when an instance of the type is dropped on a form, the Properties window will show the properties that are exposed by the type. This rich level of integration and the ease with which it’s produced are unparalleled in earlier technologies such as Win32 and COM. In this chapter, I’ll demonstrate how to use reflection to accomplish this level of integration. A method can use reflection to alter its behavior based on facts learned about another piece of code. You saw an example of this in Chapter 13. If an enumerated type has an instance of the System.FlagsAttribute applied to it, then calling ToString on an instance of the enumerated type causes the value to be treated as a bit flag instead of a single numeric value. In fact, reflection is really what custom attributes are all about. Using reflection, a method can alter its behavior based on its caller. For example, it’s possible to implement a method that performs a certain operation when called from code in the same assembly. The same method could perform slightly different operations when called from code outside the assembly. The possibilities are endless.


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.