C# › Attributes & Reflection

How does the .NET dependency injection container and ASP.NET Core's model binding use reflection and attributes together under the hood?

Advanced

Answer

ASP.NET Core scans controller action parameters and constructor parameters via reflection, matching them against registered services (for DI) or incoming request data (for model binding) — attributes like [FromBody], [FromRoute], and [Required] on those parameters guide exactly HOW reflection-based binding should interpret and validate each one at request time.
public class UsersController : ControllerBase {
  private readonly IUserService _service; // resolved via reflection-based DI
  public UsersController(IUserService service) { _service = service; }

  [HttpPost]
  public IActionResult Create([FromBody] CreateUserRequest request) { /* ... */ }
}
Real-world example Understanding how ASP.NET Core 'magically' wires up your controller's dependencies and request parameters without you writing manual binding code.

Common follow-ups: Why is reflection-heavy DI/binding done ONCE at application startup rather than on every single request, for performance reasons?