---
title: "C# Reflection – A Complete Beginner's Guide"  
description: "Reflection in C# is a powerful feature that allows you to inspect, analyze, and even modify the structure of your code (like classes, methods, propert"  
author: "Anubhav Sharma"  
published: 2025-04-22  
updated: 2025-04-22  
canonical: https://www.mindstick.com/articles/339101/c-sharp-reflection-a-complete-beginner-s-guide  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# C# Reflection – A Complete Beginner's Guide

**Reflection** in C# is a powerful feature that allows you to **inspect**, **analyze**, and even **modify** the structure of your code (like classes, methods, [properties](https://www.mindstick.com/blog/673/properties), etc.) **at runtime**. It’s part of the `System.Reflection` namespace.

## What is Reflection?

Reflection allows your program to:

1. Inspect assemblies
2. Examine types (classes, interfaces, structs)
3. Access metadata (attributes, method info)
4. Dynamically create instances
5. Invoke methods and access properties/fields

## It's widely used in:

1. Frameworks like **[Entity Framework](https://www.mindstick.com/forum/12875/how-to-use-object-query-with-a-where-and-to-retrieve-entity-record-entity-framework)**, **[ASP.NET MVC](https://www.mindstick.com/forum/12890/how-can-asp-dot-net-mvc-controller-return-an-image)**
2. **[Serialization](https://www.mindstick.com/articles/11917/serialization-in-java)/[Deserialization](https://www.mindstick.com/interview/2462/what-is-deserialization)**
3. **[Unit Testing](https://answers.mindstick.com/qa/111800/how-do-i-perform-unit-testing-and-integration-testing-for-my-code)**
4. **Plugins and dynamic [module loading](https://www.mindstick.com/forum/160364/how-to-handle-asynchronous-module-loading-in-javascript)**

## Namespaces Required

```cs
using System;
using System.Reflection;
```

## Basic Example

```cs
using System;
using System.Reflection;

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

    public void SayHello()
    {
        Console.WriteLine("Hello!");
    }
}

class Program
{
    static void Main()
    {
        Type type = typeof(Person); // Get type info

        Console.WriteLine("Class Name: " + type.Name);

        Console.WriteLine("\nProperties:");
        foreach (PropertyInfo prop in type.GetProperties())
        {
            Console.WriteLine(prop.Name);
        }

        Console.WriteLine("\nMethods:");
        foreach (MethodInfo method in type.GetMethods())
        {
            Console.WriteLine(method.Name);
        }
    }
}
```

## Common Reflection Classes

| Class | Description |
| --- | --- |
| `Type` | Represents type declarations (class, struct, etc.) |
| `PropertyInfo` | Represents a property |
| `MethodInfo` | Represents a method |
| `FieldInfo` | Represents a field |
| `ConstructorInfo` | Represents a constructor |
| `Assembly` | Represents an entire .NET assembly (DLL/EXE) |

### Getting Type Info

```cs
Type t1 = typeof(Person); // Compile time
Type t2 = obj.GetType();  // Runtime
Type t3 = Type.GetType("Namespace.Person");
```

### Invoking a Method via Reflection

```cs
Person p = new Person();
MethodInfo method = typeof(Person).GetMethod("SayHello");
method.Invoke(p, null); // Calls SayHello on p
```

### Accessing Fields & Properties

```cs
PropertyInfo prop = typeof(Person).GetProperty("Name");
prop.SetValue(p, "Alice");
Console.WriteLine(prop.GetValue(p)); // Output: Alice
```

### Creating Objects Dynamically

```cs
Type type = typeof(Person);
object obj = Activator.CreateInstance(type); // Creates new Person()

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

### Reading Attributes

```cs
[Obsolete("Use NewClass instead")]
class OldClass { }

Type type = typeof(OldClass);
object[] attrs = type.GetCustomAttributes(false);
foreach (object attr in attrs)
{
    Console.WriteLine(attr);
}
```

## Reflection Limitations

1. **Performance**: Slower than direct access.
2. **Security**: Can bypass encapsulation.
3. **Complexity**: Harder to maintain/debug.

#### Summary

| Concept | Description |
| --- | --- |
| What is it? | A way to inspect metadata at runtime |
| Key Namespace | `System.Reflection` |
| Use Cases | Serialization, DI, Frameworks, Testing |
| Main Class | `Type` |
| Dynamic Method Call | `MethodInfo.Invoke()` |
| Create Object | `Activator.CreateInstance()` |

Reflection gives C# incredible **flexibility and power**, especially for frameworks and tools. But use it wisely — only when necessary — to avoid complexity and [performance issues](https://answers.mindstick.com/qa/104810/how-to-troubleshoot-oracle-database-performance-issues).

## Read More -

- [**Access attributes using reflection**](https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/reflection-and-attributes/accessing-attributes-by-using-reflection)

---

Original Source: https://www.mindstick.com/articles/339101/c-sharp-reflection-a-complete-beginner-s-guide

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
