Reflection has always been considered an 'advanced' topic in most programming books, probably because you seldom use it unless you are in the business of writing debuggers and introspection tools such as the Javabean Development Kit. I myself would try to avoid using reflection as much as possible if there are alternative ways to do things. This particular chapter has been written in a very 'dry' style. I have simply included lots of short code examples showing how to perform certain reflection operations using reflection classes in the BCL. The intention is that you can refer back here when there is a need to write reflection codes. Like Java, reflection is largely dependent on the class libraries provided. There are no new C# keywords to be learnt in this chapter “ all you have to do is to become familiar with the C# reflection API. You could skip this chapter altogether and come back when you need 'how-to' reflection examples quickly. Reflection is possible in C# because the metadata stored, together with IL codes, in a .NET assembly provides information about the assembly itself. Most of the classes related to reflection operations are in the following namespaces: System.Reflection and System.Reflection.Emit . They are listed in Tables 16.1 and 16.2. Table 16.1. Description of the namespaces which contain significant classes for reflection
System.Type is the most important class here. Type is an abstract base class that represents a type in the CTS “ which can be a class or interface. From the view-of-classes shown in Figure 16.1, I have extracted those classes which represent different entities in C# “ these are shown in Figure 16.2. Figure 16.1. Classes which are significant in reflection. S.R. stands for the System.Reflection namespace; and S.R.E. stands for the System.Reflection.Emit namespace. Classes in unshaded boxes are found in the System namespace.
Figure 16.2. Classes which represent.NET entities.
Table 16.2. Description of the significant classes for reflection
An AppDomain object is the root of the type hierarchy of a .NET application during runtime. The AppDomain class represents a .NET application domain which is similar to a Win32 process. You can have multiple .NET assemblies executing in separate application domains simultaneously . A .NET assembly can contain one or more modules. Each module contains one or more Types (classes, interfaces) and, of course, you can find properties, events, fields, and methods within a Type . In the following sections, I shall show how to perform various reflection tasks . If you follow the code examples given, performing reflection operations in C# should be straightforward. |