Was this helpful?
Introduction to Reflection
In general, the reflection refers to the ability of a running C# program to inspect data about itself and manipulate accordingly. It is a powerful tool that lets you read whatever is there in the metadata. Recall that the metadata includes all the information about an assembly and its references.
For instance, you can use it to obtain information about the structure of assemblies and their types at runtime, a class and its members, etc. It allows you to retrieve the information relating to attributes stored in the metadata.
Reflection has few other uses too. It helps in late binding allowing you to load an assembly at runtime, look for a type, create an instance of that type, and invoke its properties and methods dynamically.
It can even be used to create entirely new types at runtime (dynamic code generation) and then use them in our code. In other words, you can emit IL code on the fly which can be executed directly.
To support reflection, the .NET class library provides a set of classes in the System.Reflection and System.Reflection.Emit namespaces. The classes in the System.Reflection namespace allow you to examine and manipulate the metadata within an assembly. They allow you to dynamically load and examine an assembly to find out what types it contains. This namespace include classes such as Assembly, Module, MemberInfo, FieldInfo, MethodInfo, ConstructorInfo, ParameterInfo, PropertyInfo, EventInfo.
The System.Reflection.Emit namespace contains the classes that let you create an assembly in memory on the fly, create a module within an assembly, define a new type for a module, define members of a new type, and emit the underlying IL code. These classes are collectively known as the Reflection API.
The MemberInfo is an abstract base class that defines a common behavior that all members, such as fields and methods, provide. For example, it offers a read-only property MemberType that returns the member's type i.e. whether it is a field, method, and so on. The Name property returns the member's name. DeclaringType returns the type of class in which the member is declared.
The MemberInfo includes an abstract method called GetCustomAttributes(). The classes derived from MemberInfo should override this method. It returns an array of all of the custom attributes attached to the member's type. If no attributes are defined, it returns an array with zero elements.
The MethodBase, FieldInfo, EventInfo, PropertyInfo etc. are the classes that are derived from MemberInfo.
An object of the MethodInfo class encapsulates the information about a particular method of a class. It includes information such as its signature, its return value etc. It also encapsulates information about the attributes that are placed on a method. The MethodInfo class does not have a public constructor. You can obtain the instance(s) of MethodInfo by making a call to the Type.GetMethods() or Type.GetMethod() method. We'll discuss the Type class soon.
You can use the MethodInfo members to get access to the method's metadata. For instance, invoke the GetParameters() method to obtain all its parameters reflected by an instance of MethodInfo. This method returns an array of ParameterInfo objects where each element represents a parameter. You can obtain the name and type of a parameter reflected by an instance of ParameterInfo.
You can invoke the underlying method represented by an instance of MethodInfo by making a call to the MethodInfo.Invoke() method.
The MethodInfo class provides properties such as IsStatic, IsPrivate, IsPublic, IsVirtual that you can use to determine whether the method represented by an instance of MethodInfo is static, public, virtual, and so on.
Similarly the ConstructorInfo class encapsulates information about a particular constructor of a class. Like MethodInfo, it is also derived from the MethodBase class. It is very similar to MethodInfo. You can obtain the instance(s) of ConstructorInfo by making a call to the Type.GetConstructors() or Type.GetConstructor() method.
You can invoke the underlying constructor represented by an instance of ConstructorInfo by making a call to the ConstructorInfo.Invoke() method.
The FieldInfo class encapsulates the information about a particular field of a class such as its type, name, its access level, whether it's static or an instance field. It also encapsulates information about the attributes that are placed on a field. This class does not have a public constructor. You can obtain the instance(s) of FieldInfo by making a call to the Type.GetFields() or Type.GetField() method.
The FieldInfo provides methods, such as SetValue() and GetValue() that allow you to set and get the value of the field reflected by the FieldInfo instance.
The PropertyInfo class encapsulates the information about a particular property of a class such as its type, name, whether the property can be read and written to. It also encapsulates information about the attributes that are placed on a property. You can obtain the instance(s) of PropertyInfo by making a call to the Type.GetProperties() or Type.GetProperty() method.
The PropertyInfo also encapsulates information about the associated set and get methods. The PropertyInfo.GetGetMethod() method returns the MethodInfo's instance that represents the get accessor for the property reflected by the PropertyInfo's instance. The PropertyInfo.GetSetMethod() method returns the MethodInfo's instance that represents the set accessor for the property reflected by the PropertyInfo's instance. You can use the PropertyInfo.SetValue() and PropertyInfo.GetValue() methods to respectively set and get the value of the underlying property reflected by the PropertyInfo's instance.
Reflection can be used to inspect and retrieve the information about an assembly. The Assembly class is discussed later.
For Part 2, click this link: Reflection in .NET - Part 2
|Apr 19, 2016
|Apr 19, 2016
|Reflection in .NET - Part 4
|Reflection in .NET - Part 3
|Reflection in .NET - Part 2
|.NET Core project option: "Produce outputs on build"
|Fastest way to serialize a POCO to JSON and deserialize JSON to a POCO
|Serialization in .NET - Part 4
|Reading appsettings.json in a .NET Core console application
|Serialization in .NET - Part 3
|Serialization in .NET - Part 2
|Serialization in .NET - Part 1