What Is Reflection In C#

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 typeof(className)


We can use the typeof(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.

What Is Reflection In C#
Figure 1: Program.cs

The preceding code returns the following output:

What Is Reflection In C#
Figure 2: Output

We can also get the information regarding all the properties of a specific class as in the following,
  1. System.Reflection.PropertyInfo[] propertyInfo = _type.GetProperties();  
  2. foreach (PropertyInfo pi in propertyInfo)  
  3. {  
  4.    Console.WriteLine(pi.Name);  
  5. }
If we include this to the preceding code then the output would be,

What Is Reflection In C#
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,

What Is Reflection In C#
Figure 4: System Reflection Class

As we can see, there is not much difference between the typeof 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.
  • 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 in all kind 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 example for late binding is the use of the dynamic keyword and use of reflection. Dynamic was introduced in C# 4.0 and introduced to achieve dynamically static object that the compile-time detection but at runtime it detects the object actualy type depending on the right-hand object-type and convert it to a strong type.

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

What Is Reflection In C#
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 reading the meta-date information of an assembly or when an actual type is not known. It 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.