Mastering .NET Reflection in C# - With Real Examples

Mastering .NET Reflection in C# - With Real Examples

Reflection in .NET is a powerful feature that allows you to inspect and manipulate the structure of assemblies, types, properties, methods, and more at runtime. This blog explores practical examples of how to use reflection in C# to dynamically invoke methods, access private members, and call constructors.

What is .NET Reflection?

Reflection allows you to:

  • Inspect assemblies, classes, properties, and methods at runtime
  • Invoke methods dynamically
  • Access private members for testing or special use cases
  • Dynamically create objects

Example: Inspecting a Class

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public void SayHello()
    {
        Console.WriteLine("Hello!");
    }
}
Type type = typeof(Person);

foreach (PropertyInfo prop in type.GetProperties())
{
    Console.WriteLine($"{prop.Name} ({prop.PropertyType.Name})");
}

foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
    Console.WriteLine(method.Name);
}

Invoke a Method Dynamically

MethodInfo method = type.GetMethod("SayHello");
method.Invoke(personInstance, null);

Invoke a Method with Parameters

public class Calculator
{
    public int Add(int a, int b) => a + b;
}

MethodInfo method = typeof(Calculator).GetMethod("Add");
object result = method.Invoke(new Calculator(), new object[] { 5, 7 });

Access Private Members

FieldInfo field = typeof(User).GetField("password", BindingFlags.NonPublic | BindingFlags.Instance);
field.SetValue(userInstance, "newPassword");

Invoke Constructors

ConstructorInfo ctor = typeof(Person).GetConstructor(new[] { typeof(string) });
object obj = ctor.Invoke(new object[] { "Aslyne" });

Learn with Me - 1-on-1 Coding Classes

Want to master topics like Reflection and more in C# and .NET? I offer 1-on-1 personalized programming classes for all levels. Whether you're just getting started or preparing for job interviews, I’ll guide you step-by-step.

Contact Me:
Website: https://supernovaservices.com/
WhatsApp: +91-9331897923

Happy Coding!

Class Guidelines for Effective 1-on-1 Learning

To keep every session productive and distraction-free, please follow these simple guidelines:

  • Quiet Environment: Join from a calm, private room with minimal background noise. Avoid public or noisy places.
  • No Interruptions: Inform family/roommates in advance. Keep doors closed during class.
  • Mobile on Silent / DND: Set your phone to Silent or Do Not Disturb to prevent calls and notifications.
  • Be Fully Present: Do not multitask. Avoid attending to other calls, visitors, or errands during the session.
  • Stable Setup: Use a laptop/desktop with a stable internet connection and required software installed (Visual Studio/.NET, SQL Server, etc.).
  • Punctuality: Join on time so we can utilize the full session effectively.
  • Prepared Materials (If any): Keep project files, notes, and questions ready for quicker progress.

Following these guidelines helps you focus better and ensures I can deliver the best learning experience in every class.

Schedule a Quick 10-Minute Call

I prefer to start with a short 10-minute free call so I can understand:

  • Your learning objectives and career goals
  • Your current skill level
  • The exact topics you want to learn

Why? Because course content, teaching pace, and fees all depend on your needs — there’s no “one-size-fits-all” pricing. Please leave your details below, and I’ll get back to you to arrange a convenient time for the call.



Google Review Testimonials

.NET Online Training
Average Rating: 4.9
Votes: 50
Reviews: 50