Skip to content
AgileLogics
4 min read

Reflection & Attributes

Prerequisites: Pattern matching in depth

Reflection inspects types, methods, and properties at runtime — the mechanism frameworks like ASP.NET Core and xUnit use to discover your controllers and [Fact]-annotated tests without you wiring them up by hand, directly parallel to Java's reflection and annotations.

Type type = typeof(string);
Console.WriteLine(type.Name); // String

foreach (var method in type.GetMethods()) {
    Console.WriteLine(method.Name);
}

[AttributeUsage(AttributeTargets.Method)]
class ImportantAttribute : Attribute {}

[Important]
void CriticalTask() {}

An attribute ([Important], [Fact], [HttpGet]) is C#'s equivalent of a Java annotation — metadata reflection (or the framework itself) reads later.