What is Reflection In C#

Introduction to reflection

Reflection is used to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, reflection enables you to access them.

Using the type of (className)

We can use the type of (className) function to get the information of all the methods, members and so on that is a part of the specific class. An example can be seen in the following.

Type of className(1)-

Figure 1. Program.cs

The preceding code returns the following output.

Output-

Figure 2. Output

We can also get the information regarding all the properties of a specific class as in the following

If we include this in the preceding code then the output would be

Output 2-.

Figure 3. Output2

Using GetType()

We can also get an assembly inspected by using the GetType method of the System. Reflection class. The code for that would be as in the following,

Using get type program-

Figure 4. System reflection class

As we can see, there is not much difference between the type of and GetType methods. What is the functional difference between the two? Let's figure it out. typeOf is a C# keyword used when you have the name of the class. It is calculated at compile time and thus cannot be used on an instance that is created at runtime. GetType is a method of the object class that can be used on an instance.

Early binding vs late binding

  • Performance will be good in Early binding.
  • The application will run faster in Early binding since no boxing or unboxing is done here, but in late binding, we need type conversion as it will be decoded at run time.
  • Easier to write the code in Early binding, since the intelligence will be automatically populated.
  • Less chance of errors in early binding, since the syntax is checked that flags an error at compile time itself but for late binding there are chances of run time exceptions.
  • Late binding would support all kinds of versions due to handing at run time.
  • Minimal impact of code in future enhancements, if late binding is used.

Late binding using reflection

Late bindings can be done by using reflection. For example, in some applications, we don't know which assembly to load during compile time, so we ask the user to enter the assembly name and type during run time and the application can load the assembly. For this purpose, the System.Reflection.Assembly type offers three static methods that allow you to explicitly load an assembly, Load, LoadFrom, and LoadWithPartialName. These methods are something similar to the LoadLibrary Win32 API. Our System. Reflection namespace will work with assemblies and metadata.

In late binding, the compiler does not know what kind of object or actual type of an object or which methods/properties an object contains so it binds the compile-time checking that was handled by the run-time. The most popular examples of late binding are the use of the dynamic keyword and the use of reflection. The dynamic was introduced in C# 4.0 and introduced to achieve the dynamic static object that the compile-time detection but at runtime it detects the object actually type depending on the right-hand object type and converts it to a strong type.

Please see the following example of using Late Binding in C# by Reflection.

Late Binding-

Figure 5. Example of late binding

You would need to change the path of the DLL as in your system. As you see from the preceding code the GetType() method/information is invoked during runtime.

Advantages of using late binding

  • Late binding is very useful in reading the metadata information of an assembly or when an actual type is not known.
  • It is also useful when an actual type is not known. Late binding is more certain to be version-independent since everything is decided at run time.
  • Please find the code attached for more reference and a better understanding.


Similar Articles