Reflection
Saravanakumar R
Posted on November 20, 2024
Reflection in C# is the process of inspecting the metadata of types, methods, properties, and other members at runtime. It allows you to interact with objects dynamically and access information about assemblies, types, methods, properties, and more. The System.Reflection namespace provides classes like Type, MethodInfo, PropertyInfo, etc., to work with these elements.
Imagine you have a box, and inside that box, there's a really cool toy car. But you can't open the box to see the car directly. However, you have a special tool called "reflection."
When you use reflection on the box, it's like using the magic mirror on the toy car. Reflection allows you to learn things about the car without actually opening the box. You can find out the color of the car, how many wheels it has, and even what sounds it can make when you push it.
Similarly, in programming, sometimes programmers have objects (like the toy car) that they can't see directly. But with the help of reflection, they can still learn a lot about those objects. They can find out their names, what they can do, and even change their behaviors, all without having to open the box (or directly access the object in their code).
Reflection is like a special tool that lets programmers explore and manipulate objects in their computer programs, just like you can explore and learn about the toy car inside the box using the magic mirror.
How to use reflection to find the two date fields in an employee object and assign today's date to those fields
using System;
using System.Reflection;
public class Employee
{
public string Name { get; set; }
public int Age { get; set; }
public DateTime JoiningDate { get; set; }
public decimal Salary { get; set; }
public DateTime BirthDate { get; set; }
// ...other properties
}
public class Program
{
public static void Main()
{
Employee employee = new Employee();
UpdateDateFields(employee);
// Print the updated values
Console.WriteLine("Joining Date: " + employee.JoiningDate);
Console.WriteLine("Birth Date: " + employee.BirthDate);
}
public static void UpdateDateFields(object obj)
{
PropertyInfo[] properties = obj.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
if (property.PropertyType == typeof(DateTime))
{
property.SetValue(obj, DateTime.Today);
}
}
}
}
Interview
- How can you use Reflection to get the methods of a class?
You can use the GetMethods() method of the Type class to get the methods of a class. Here's an example:
Type type = typeof(SampleClass); MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
This code will display the names of both public and private methods of the SampleClass
- How can you access the properties of a class using Reflection? You can use the GetProperty() method of the Type class to access a property
Type type = typeof(SampleClass);
PropertyInfo property = type.GetProperty("Name");
3.What are some use cases of Reflection in C#?
Dynamic Object Creation: **You can create objects at runtime using Activator.CreateInstance.
**Serializing/Deserializing: Reflection is useful when dynamically reading or writing properties for serialization or deserialization.
Testing and Mocking: In unit tests, reflection can be used to access private members or methods.
Dependency Injection: Reflection can inspect and resolve dependencies in dependency injection frameworks.
Frameworks and Libraries: Many libraries, such as ORM frameworks (like Entity Framework), use reflection to map data between objects and database tables.
- Can you explain how to use Activator.CreateInstance() with Reflection? Answer: Activator.CreateInstance() is used to create an instance of a class dynamically using reflection. Hereโs how to use it:
Posted on November 20, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.