Late Binding

Late binding is the instantiation and manipulation of types at run time. This is most often used by scripting engines and applications that accept pluggable modules—for example, an application server that allows modules to be loaded dynamically.

Instantiation

Instances of types can be created either by using the System.Activator class or by calling the Invoke method of a ConstructorInfo instance obtained from System.Type.GetConstructor. The methods are the equivalents of the java.lang.Class.newInstance and java.lang.reflect.Constructor.newInstance methods.

The Activator class accepts an array of objects that will be used as constructor arguments and attempts to locate a constructor that offers the best possible match. Using the ConstructorInfo class ensures that a specific constructor is used for instantiation.

The following example demonstrates how to instantiate a StringBuilder using the Activator and ConstructorInfo classes:

Type x_type = typeof(System.Text.StringBuilder);

object first_instance = Activator.CreateInstance(x_type,
    new object[] {"C# for Java Developers"});

ConstructorInfo x_constructor = x_type.GetConstructor(
    new Type[] {typeof(System.String)});
object x_second_instance = x_constructor.Invoke(
    new object[] {"C# for Java Developers"});

Manipulation

In the preceding example, we could have cast the instantiated object to StringBuilder and called the members as for a normally instantiated type. This is the approach usually adopted if types are required to implement a specific interface to provide functionality.

However, it’s also possible to call members using the reflection classes described in the preceding section. Table 12-13 details the methods available.

Table 12-13. Important .NET Reflection Methods

Method

Description

ConstructorInfo.Invoke()

Creates a new instance of the declaring type

MethodInfo.Invoke()

Calls the method

PropertyInfo.SetValue()

Gets or sets the value of a property

PropertyInfo.GetValue()

 

EventInfo.AddEventHandler()

Directly adds and removes handlers and exposes the methods that are used to add and remove handlers and raise the event

EventInfo.RemoveEventHandler()

 

EventInfo.GetAddMethod()

 

EventInfo.GetRemoveMethod()

 

EventInfo.GetRaiseMethod()

 

FieldInfo.GetValue()

Gets or sets the value of a field

FieldInfo.SetValue()

 

The following example demonstrates how to invoke a method via the MethodInfo class:

Type x_string_type = typeof(System.Text.StringBuilder);

object first_instance = Activator.CreateInstance(x_string_type,
    new object[] {"C# for "});

MethodInfo x_method = x_string_type.GetMethod("Append",
    new Type[] {typeof(String)});

x_method.Invoke(first_instance, new object[] {"Java Developers"});

The example uses the Activator class to create a new instance of StringBuilder and then locates and invokes the Append method.

..................Content has been hidden....................

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