Reflection can be used to dynamically generate an instance of a type, bind a type to an existing object, or retrieve a type from an existing object so you can access its fields and attributes and call its methods.
A reflection is a potent tool in C# that allows you to examine and alter the behavior of types, objects, and assemblies in real time. Reflection offers a technique for learning about types and the individuals that make them up as well as for dynamically invoking object fields, attributes, and methods.
How to use Reflection in C#:
With the help of C# Reflection, the program can act independently and obtain information. It dynamically invokes assembly methods and efficiently finds all varieties of assemblies. The major important class which is used in reflection is System. Type class is an abstract class representing a type called CTS (Common Type System). With the help of this class, we can locate the types we used in namespace modules and confirm if a given type is a value type or a reference type.
C# provides System.Reflection namespace using which we can perform reflection. The System. Reflection namespace contains the classes:
- Type
- MemberInfo
- ContructorInfo
- FieldInfo
- MethodInfo