C# Mastery

Reflection and Attributes: Reading Metadata at Runtime

1 Views Updated 5/4/2026

Reflection & Metadata

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.

1. Inspecting Types

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}");
}

2. Attributes: The "Stickers" of C#

Attributes allow you to tag your code with extra information (Metadata) that can be read later by Reflection.

[Serializable]
[Author("Sandeep")]
public class Order { ... }

3. The Performance Tax

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.

4. Interview Mastery

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."

C# Mastery
1. Modern C# & Framework Fundamentals
Introduction to the .NET Ecosystem & Runtime (JIT, CLR) C# 12/13 Top-Level Statements & Global Usings Variables, Data Types, and Value vs Reference Deep Dive Mastering Nullable Reference Types & Null Safety The Magic of Strings (Interpolation, Verbatim, and Immutability)
2. Control Flow & Logical Structures
Advanced Pattern Matching (Switch Expressions) The Precision of Numbers: Checked vs Unchecked Math Iterators: Foreach, Yield Return, and Deferred Execution Defensive Programming: Guard Clauses and Exception Mastery
3. Object-Oriented Mastery
Classes vs Structs vs Records (Which to use when?) Mastering Primary Constructors & Object Initializers Interface Architectures: Default Implementations & Segregation Polymorphism vs Composition (The Architect's Dilemma) Partial Classes, Extension Methods, and Static Classes
4. Functional C# & Collections
Delegates, Func, Action, and Predicates Lambda Expressions & The Evolution of Linq Dynamic Array Management: List<T>, Dictionary, and HashSet Custom Collections & Yielding Data streams
5. Asynchronous & Parallel Programming
Async/Await Deep Dive: Task Lifecycle & Thread Safety Synchronization Context & Avoiding Deadlocks Parallel.ForEach vs PLINQ vs Tasks CancellationToken Mastery: Surgically Aborting Operations
6. Advanced Engineering & High Performance
Generics & Constraints: Building Type-Safe Libraries Reflection and Attributes: Reading Metadata at Runtime Dependency Injection Internals (ServiceCollection from scratch) High-Performance Memory: Span<T> and ReadOnlySpan<T> Garbage Collection Segments & LOH Internals Managing Unmanaged Resources: IDisposable & Finalizers Introduction to Source Generators & Interceptors C# Interview Masterclass: Architect-Level Explanations