Roadmap › C# Language — Basics to Advanced › Module: Modern C# and Reflection
Lessons in this course (38)

37. Reflection, attributes and dynamic

AdvancedMediumLesson 37 of 38
csharp
[AttributeUsage(AttributeTargets.Property)]
public sealed class MaskAttribute : Attribute { }

public class User { public string Name { get; set; } = ""; [Mask] public string Pan { get; set; } = ""; }

foreach (var prop in typeof(User).GetProperties())
{
    bool masked = prop.GetCustomAttribute<MaskAttribute>() is not null;
    Console.WriteLine($"{prop.Name}: {(masked ? "****" : prop.GetValue(user))}");
}

dynamic d = JsonSerializer.Deserialize<ExpandoObject>(json)!;  // resolved at runtime (DLR)

Reflection is slow-ish and not trim/AOT-friendly; modern .NET prefers source generators (e.g. System.Text.Json source-gen, [LoggerMessage], [GeneratedRegex]).