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 calledAge, and a method calledSave().”
🧩 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:
- JSON Serializers: To know which properties to save to a file.
- ORMs (EF Core): To know how to map your class to a SQL table.
- 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
- Reflection: Looking at code metadata at runtime.
- Type: The object that describes a class.
- Attribute: A label stuck to a class or property.