Examples of using the reflection based on late binding
This topic is a continuation of the following topics:
- Reflection of types. Getting type metadata. The System.Reflection namespace. Class System.Type. Ways to get information about types. Operator typeof
- 20.5. Late binding. Method call. Example. Class System.Activator. Method Invoke()
1. Examples of using the reflection based on the late binding mechanism
As you know, the main advantage of reflection is the ability to inspect an object, information about which was unknown at the compilation stage. This is due to late binding. Below are some examples of how reflection can be used.
- Suppose a situation has arisen in the program when an object is received from an external medium that is not in the namespace of our program. Such an object can be received as a sequence of bytes from some network connection. It is only known that this object is a class. The task arises to determine the components of this class, then, based on these components, form an instance of the class and run the necessary methods.
- Reflection is effective in component programming in Rapid Application Development (RAD) systems. In such systems, components that are added to a program or form are represented as icons and the like. For the placed component, the corresponding program code is loaded, which forms the functionality of this component. With the help of reflection, you can dynamically add this functionality during program execution.
- The ability to create and use objects on remote platforms. This capability is called Remote Method Invocation (RMI). Objects are distributed across multiple computers. The computer using the remote object uses this object by reflection.
- Almost all modern .NET frameworks are based on reflection.
- The ildasm.exe utility itself loads the assembly, defines methods in it, their parameters, and the like. The utility uses reflection to define assembly metadata.
- With the help of reflection, the program can change its structure by adding additional modules to its functionality.
- Reflection is widely used in .NET to obtain information about the attributes of classes. For example, a situation is possible when you need to determine whether an object of a certain class supports serialization/deserialization. To do this, the presence (absence) of the Serializable attribute in the class is determined.
- In modern systems of visual development of applications, there is support for their modification by adding new components. For example, in the Delphi programming system it is possible to develop your own component, form this component into an object module (*.DCL is the same as *.DLL) and add this component to the Delphi component palette. The system, using reflection, will determine the types, the names of the types, the constituents of these types, which are used in the component. After closing and then launching the Delphi visual application development system, this component will already be loaded automatically, since information about it will be saved in the Delphi configuration files.
2. Late binding. An example of getting some object from the network and using it. Figure
Figure 1 depicts one of the features of reflection, which determines the internal composition of an object based on the late binding mechanism.
Suppose that during the execution of the program, some object SomeObject was received from the external environment. The content of the object is unknown.
Through the use of reflection, it is possible to:
- get exclusive information about the types implemented in the object;
- run the object’s methods for execution.
Figure 1. Inspecting an object using reflection
3. Late binding. An example of including types from another assembly. Picture
You can use the late binding mechanism to define the constituents of other assemblies. Figure 2 shows an example of getting data from another assembly. Let some file storage be given, which contains some assembly named SomeAssembly.dll. The contents of the assembly are unknown. Thanks to reflection, it is possible to determine the internal content of the assembly and add it to the program: types (classes, interfaces, structures, etc.) and elements in these types (methods, properties, internal fields, etc.).
Figure 2. Connecting an external assembly to the main program using reflection