Reflection is the ability of your program to "look in the mirror" and inspect its own code structure at runtime. While it is incredibly powerful for building frameworks (like Dependency Injection containers or JSON Serializers), it carries a heavy performance cost and must be used with caution.
You can discover properties, methods, and private fields of any object even if you've never seen the class before.
var user = new User();
Type type = user.GetType();
foreach(var prop in type.GetProperties())
{
Console.WriteLine($"Found Property: {prop.Name}");
}
Attributes allow you to tag your code with extra information (Metadata) that can be read later by Reflection.
[Serializable]
[Author("Sandeep")]
public class Order { ... }
Reflection is slow because the CLR has to scan the assembly metadata and resolve strings into memory pointers manually. A method call via Reflection can be 100x slower than a direct method call.
Q: "If Reflection is so slow, how do high-performance frameworks like AutoMapper or JSON.NET work so fast?"
Architect Answer: "The secret is 'Emit' and 'Caching.' A professional framework uses Reflection exactly ONCE to inspect the type. Then, it uses `System.Reflection.Emit` to generate raw IL (Intermediate Language) code in memory essentially writing a new C# class dynamically that performs the mapping. This new dynamic class is then cached. All subsequent calls bypass Reflection entirely and run as native, top-speed compiled code."