C# Reflection: Everything you need to know

01 maart 2021 om 10:00 by ParTech Media - Post a comment

C# Reflection is a procedure by which a code or program can observe and upgrade its behaviour and structure. This method can help us to analyze and examine our codes. Also, it gives us the flexibility to change the details of an application during runtime by offering huge potential.

In this post, we are going to understand the A-Z of C# Reflections by deep diving into various examples of it.

Table of Contents

  1. Example of Reflection
  2. Significance of Reflection
  3. How to view metadata using Reflection?
  4. Common Classes of System.Reflection
  5. What is Type Class?
  6. How to instantiate a Class using Reflection?
  7. Reflection on methods
  8. What is Assembly Class?
  9. Conclusion

Example of Reflection

We use Reflection to describe the metadata of methods, types and the fields of a program. System.Reflection is a namespace which helps us to get data about loaded assemblies and its elements. It can be methods, value types and classes.

We need to include the namespace System.Reflection to use Reflection in our codes. We can utilize Reflection to develop a type instance dynamically.

Below is an example that is nothing but a common problem of changing variable value during runtime by only knowing its name.

Significance of Reflection

We can use Reflection to improve the quality of our codes. These are the situations where we can utilize Reflection:

  • Suppose we want to access the attribute in the metadata of our codes. We can use Reflection.
  • We can implement Reflection to examine & instantiate the assembly types.
  • If we want to create new types during runtime we can use System.Reflection.Emit. It will also help us to perform additional tasks by utilizing those new topics.
  • We can use Reflection to access the methods of types that are created during runtime. We also can utilize it to perform late binding.

How to view metadata using Reflection?

In the previous section, we had seen that by using Reflection you can view the attribute information.

Reflection gives developers the flexibility to view the metadata of attributes. Here we need to use the MemberInfo object from the class System.Reflection.

We need to initialize this to discover the attributes. Here is an example depicting the same:

Common Classes of System.Reflection

Assembly: Developers use it to describe a reusable assembly. It also has self-describing nature to describe the construction of the runtime application. We can manipulate, load and investigate an assembly by accessing the methods of this class.

Module: Developers can use this class to perform Reflection on a module. We can access any module from the multifile assembly.

AssemblyName: It is used to identify the assembly with multiple details such as Version Number, Simple name, Cryptographic-key pair etc.

ConstructorInfo: This class is used for describing the class constructor and helps us to access the metadata.

MethodInfo: It describes the information regarding class methods.

ParameterInfo: Developers use this class to describe the method parameters.

EventInfo: This class stores information for different events. Programmers can use this class to examine different events and bind them.

PropertyInfo: It provides information regarding properties and we can access the metadata of a property.

MemberInfo: It gives information regarding the member’s attribute. It includes fields, events, constructors, properties etc. We can also access the member’s metadata using this.

Let us consider an example. A string is loaded in type t below by using the method typeof().

Here we are applying Reflection over t for finding important information regarding the string class.

Here we are using Reflection to describe all metadata associated with this code. It consists of methods, classes and various parameters.

What is Type Class?

This class is one of the fundamental classes of Reflection. It can provide details regarding a type or a module. It can also help us to get the runtime data of an assembly. We can easily obtain the reference of an object’s type. Every class has a method called GetType(). Also, we have the typeof() method to access the information globally. Here is an example where we apply these two approaches:

Here we can use the method GetType() to get necessary information of our own declared variable. Also, we have used the typeof() method.

How to instantiate a Class using Reflection?

Till now we have worked with examples where the objects or types are instantiated. We can also instantiate at runtime using Reflection by knowing the class name. There are several ways to accomplish this job. Here we are using a constructor’s reference which we want to use. Here is the example explaining this:

Here we have used the class TestClass for implementation. This class includes a public method and a private field. We can use the method to return the private property’s value. It also describes the information of the added parameters. Our objective is to create a new instance of this class. We will call it TestMethod.

Here developers have the flexibility to implement the typeof() method on the class. We can also do this differently by calling the name of the required class by giving the reference of the assembly where it was initialized.

We want to get the default constructor by implementing the method GetConstructor(). Here we also need to pass the parameter System.Type.EmptyTypes. Now, we can get the reference of the constructor and call the method Invoke() for creating a new instance of the class TestClass.

Reflection on methods

We can use the method GetMethod() to get the information of a method. It provides us with a reference. Further, the reference data is stored in the object System.Reflection.MethodInfo. We can search the public method by using its name.

We can get a reference array from the GetMethods(). It gives us the details of all methods.

What is Assembly Class?

We can get the Assembly Class from System.Reflection namespace. It is used to get Assembly related information for manipulating. This class gives us the flexibility to load assemblies and modules during the run-time. This class connects with the PE file for fetching details. After loading the class Assembly, developers can find the type details.

Conclusion

Reflection allows the developers for using the codes that were unavailable during compilation. It helps applications to get & manipulate its metadata. Using Reflection, programmers can find details that can be generally found in the class viewer.

Nieuwste