Skip to content

Module 6: Reflection (The Mirror)

📚 Module 6: Reflection

Course ID: DOTNET-104
Subject: The Mirror of Code

Reflection allows a program to look at its own structure (Classes, Methods, Properties) and even change them at runtime.


🏗️ Step 1: Metadata (The “Identity Card”)

Every class in .NET carries an ID Card.

  • It says: “My name is User, I have a property called Age, and a method called Save().”

🧩 The Analogy: The Mirror

  • Normal Code: You look at an apple and eat it.
  • Reflection: You hold a Mirror up to the apple. You see its shape, its color, and its weight. You can even decide to change its color in the reflection!

🏗️ Step 2: Why do we use it?

You rarely use reflection for simple logic. We use it for Tools:

  1. JSON Serializers: To know which properties to save to a file.
  2. ORMs (EF Core): To know how to map your class to a SQL table.
  3. Automated Testing: To find all methods marked with [Test].

🏗️ Step 3: Attributes (The “Label Maker”)

Attributes allow you to stick Labels onto your code.

  • [Serializable]
  • [Required]
  • [Key]

🧩 The Analogy: The Sticky Note

  • You put a Sticky Note on a box that says: “FRAGILE.”
  • The shipping company (The Framework) reads the note and treats the box differently.

🥅 Module 6 Review

  1. Reflection: Looking at code metadata at runtime.
  2. Type: The object that describes a class.
  3. Attribute: A label stuck to a class or property.