13. Attributes & Reflection
Part 1: Understanding the Metadata System
The Big Picture
┌────────────────────────────────────────────────────┐
│ METADATA SYSTEM │
│ │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ ATTRIBUTES │────────>│ REFLECTION │ │
│ │ (Add data) │ │ (Read data) │ │
│ └──────────────┘ └──────────────┘ │
│ │ │ │
│ │ │ │
│ v v │
│ [Validation] GetCustomAttribute() │
│ [Route("api")] GetProperties() │
│ [Obsolete] GetMethods() │
│ [Serializable] Activator.CreateInstance() │
│ │
└────────────────────────────────────────────────────┘
What Are Attributes?
What Is Reflection?
Part 2: Built-in Attributes
1. [Obsolete] - Mark Old Code
2. [Serializable] - Allow Serialization
3. [DllImport] - Call Native Code
4. [CallerMemberName] - Get Caller Info
5. [Flags] - Enum as Bit Flags
6. [Conditional] - Conditional Compilation
Part 3: Creating Custom Attributes
Step 1: Define Attribute Class
Step 2: Control Where Attribute Can Be Used
AttributeTargets Options
Step 3: Using Custom Attributes
Part 4: Reflection Basics
Getting Type Information
Reflection Classes
Part 5: Reading Attributes
Understanding Attribute Retrieval Methods
Method
Returns
Use When
Single Attribute on Class
Multiple Attributes on Class
Attributes on Properties
Attributes on Methods
Attributes on Parameters
Part 6: Attribute Filtering Patterns
Pattern 1: Find All Classes with Specific Attribute
Pattern 2: Find Classes Where Attribute Matches Condition
Pattern 3: Get All Attribute Values Across All Classes
Pattern 4: Group Classes by Attribute Value
Pattern 5: Complex Filtering (Version Range)
Part 7: Common Mistakes & Best Practices
❌ Mistake 1: Forgetting to Cast (Legacy Method)
❌ Mistake 2: Using Singular Method for Multiple Attributes
❌ Mistake 3: Not Checking for Null/Empty
✅ Best Practice: Use Generic Methods
Part 8: Real-World Patterns
Pattern 1: Validation Framework
Pattern 2: Simple ORM (Object-Relational Mapping)
Pattern 3: API Route Registration
Part 9: Working with Types
Creating Instances Dynamically
Invoking Methods Dynamically
Getting/Setting Properties Dynamically
Working with Generic Types
Part 10: Performance Considerations
Reflection is SLOW
Caching Pattern
Part 11: Common Reflection Methods Reference
Type Information
Attributes
Constructors
Properties
Methods
Fields
Assemblies
Quick Reference Summary
When to Use Attributes
When to Use Reflection
How Frameworks Use This
Last updated