Chapter 12. Reflection

Reflection exposes type information at run time, supporting powerful scripting technologies and applications that allow plug-in modules, such as application servers. This chapter details the .NET support for reflection, demonstrating how to gain type information and programmatically instantiate and manipulate types.

Dealing with Types

Java and .NET both encapsulate the details of a type in a single class: Java relies on java.lang.Class, and .NET defines System.Type. The following sections describe how to obtain System.Type instances.

Local Types

Local types are those that are directly available within the scope of an application. In Java, local types are those that can be found in the classpath; in .NET, local types are those that are contained in the application assemblies and the assemblies referenced at compile time. For more information about assemblies and references, see Chapter 3.

Table 12-1 shows the intrinsic support available in both platforms for obtaining local type details.

Table 12-1. Comparison Between the Java and .NET Intrinsic Local Type Support

Java

.NET

Comments

Object.getClass()

Object.GetType()

Returns details of a type from an instance of the type

<ClassName>.class

typeof(<ClassName>)

Returns type information from the type name

Class.forName(string)

Type.GetType(string)

Return details of a type specified by a string representing the type name

For example, the System.String class is a local type because the assembly that contains it, mscorlib.dll, is automatically referenced by the C# compiler. The following example demonstrates how to obtain type information for this class:

String myString = "this is my string";
System.Type stringType1 = myString.GetType();
System.Type stringType2 = typeof(System.String);
System.Type stringType3 = Type.GetType("System.String");

Foreign Types

Foreign types are those that are not directly available to an application. Java foreign types are those whose class files are not included in the application classpath; .NET foreign types are those contained in assemblies that were not referenced at compilation.

The Java java.lang.Class class includes an overloaded form of the forName method, which accepts a java.lang.ClassLoader as an argument. The .NET Framework makes similar functionality available through the System.Reflection.Assembly class.

The Assembly class defines several static methods that can be used to obtain Assembly instances, as detailed in Table 12-2.

Table 12-2. Static Methods Available from the Assembly Class

Assembly Method

Description

GetAssembly()

Returns the assembly in which a specified type is contained.

GetCallingAssembly()

Returns the assembly that invoked the method that is currently being executed.

GetEntryAssembly()

Returns the first assembly that was executed by the common language runtime.

GetExecutingAssembly()

Returns the assembly that contains the currently executing code.

Load()

Loads an assembly from a file by name or from a byte array held in memory. File-based assemblies must be locatable in the application search path. See Appendix B, for more details about assembly search paths.

LoadFrom()

Loads an assembly from a specified file.

The Assembly.GetType method returns an instance of System.Type for a named type; the GetTypes method returns an array of System.Type objects containing all of the types contained within the assembly.

The following example shows how to list the names of all of the types contained in the mscorlib assembly:

Assembly x_assembly = Assembly.Load("mscorlib");
Type[] x_types = x_assembly.GetTypes();
foreach (Type x_type in x_types) {
    Console.WriteLine("Name: " + x_type.Name);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset