Introduction
In this blog, we will learn about the Reflections in C#, so let's begin with an introduction of the Reflections, and then we will learn how we use them with their examples.
In C#, Reflection is used to get metadata on types at runtime. In another way, you can use Reflection to dynamically analyze metadata of the types in your program — you can get information on the loaded assemblies, and the types declared in them—the namespace System. Reflection enables you to obtain data about the loaded assemblies and the elements within them like classes, methods, and value types.
What is Reflection?
Objects that describe assemblies, modules, and types are provided by Reflection. You may use Reflection to dynamically generate a type instance, link a type to an existing object, or get a type from an existing object and use its methods, fields, and attributes. Reflection allows you to access characteristics in your code if you use them.
Here's a primary reflection example that shows how to get the type of a variable using the GetType() function, which is inherited by all types from the Object base class.
using System;
public class ReflectionExample
{
public static void Main()
{
int a = 10;
Type type = a.GetType();
Console.WriteLine(type);
}
}
Output:
System.Int32. |
Uses
In the following scenarios, Reflection is beneficial:
- When you need to access metadata properties in your software.
- In an assembly for inspecting and instantiating types.
- For creating new types at the run time.
- Accessing methods on types constructed at run time to conduct late binding.
Commonly Used Classes
Some of the Commonly Used Classes are as follows:
Class |
Description |
Assembly |
explains an assembly, which is a reusable, versionable, and self-describing building element |
AssemblyName |
Identifies a certain assembly by its name. |
ConstructorInfo |
Describes a class constructor and provides metadata access. |
MethodInfo |
Provides information about the class method as well as access to its metadata. |
ParameterInfo |
Describes a method's arguments and provides access to its information. |
EventInfo |
Describes the information about the event and provides access to its metadata. |
PropertyInfo |
Discovers a property's characteristics and offers access to property metadata. |
MemberInfo |
It gets information about a member's qualities and gives you access to their metadata. |
Example
The namespace for Reflection is Reflection.System. The assembly module, MemberInfo, PropertyInfo, MethodInfo, ConstructorInfo, FieldInfo, EventInfo, and so on, is defined in the Reflection namespace.
using System;
using System.Reflection;
public class MyClass
{
public virtual int AddNumb(int numb1,int numb2)
{
int result = numb1 + numb2;
return result;
}
}
class MyMainClass
{
public static int Main()
{
Console.WriteLine ("Reflection.MethodInfo");
// Create MyClass object
MyClass myClassObj = new MyClass();
// Get the Type information.
Type myTypeObj = myClassObj.GetType();
// Get Method Information.
MethodInfo myMethodInfo = myTypeObj.GetMethod("AddNumb");
object[] mParam = new object[] {5, 10};
// Get and display the Invoke method.
Console.Write("First method - " + myTypeObj.FullName + " returns " +
myMethodInfo.Invoke(myClassObj, mParam) + "\n");
return 0;
}
}
Output:
Reflection.MethodInfo First method - MyClass returns 15 |