C#.NET — Inspecting attributes: at which stages can attributes be examined or queried? Choose all that truly apply: 1) Link-time 2) Compile-time 3) Run-time 4) Design-time (e.g., by tooling in IDEs)

Difficulty: Easy

Correct Answer: 3, 4

Explanation:


Introduction / Context:
Attributes in C#.NET are metadata attached to code elements (assemblies, types, members, parameters, etc.). Understanding when and how they can be inspected helps developers choose the right mechanism (reflection, analyzers, or tooling) for validation, diagnostics, and behavior changes.



Given Data / Assumptions:

  • We are considering standard .NET attribute usage.
  • “Inspection” means reading attribute metadata.
  • Stages under consideration: link-time, compile-time, run-time, and design-time.


Concept / Approach:
Attributes are stored in metadata and can be retrieved via reflection at run-time. Many IDEs and tools (design-time) read attributes to alter experience (e.g., designers, code generators). While compilers may react to certain attributes, general attribute inspection is typically done at run-time or by design-time tools.



Step-by-Step Solution:

Evaluate 1) Link-time: .NET does not have a separate “linker-inspection” phase for attributes in typical workflows; not generally applicable.Evaluate 2) Compile-time: Some attributes influence compilation (e.g., Obsolete), but broad attribute inspection is not generally performed by user code at compile-time.Evaluate 3) Run-time: Reflection (MemberInfo.GetCustomAttributes, etc.) reads attributes. This is a standard and widely used mechanism.Evaluate 4) Design-time: Visual designers and analyzers can read attributes to guide tooling behavior, so design-time inspection is valid.


Verification / Alternative check:
Write a short program that calls GetCustomAttributes on a type or member at run-time; observe returned attribute instances. In IDEs, attributes like Browsable or DisplayName change designer behavior at design-time.



Why Other Options Are Wrong:

  • Link-time: not a standard inspection phase in managed .NET builds.
  • “All of the above” overstates link/compile-time general inspection.


Common Pitfalls:
Confusing compiler reaction to specific attributes with general-purpose attribute inspection available to user code.



Final Answer:
3, 4

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion