Junior · Explain · Attribute data versus attribute execution

How does reading CustomAttributeData differ from requesting an attribute instance?

Think through your answer before opening the explanation.

Runtime & concurrency

Reveal answer and reasoning

CustomAttributeData describes recorded constructor and named arguments without invoking the custom attribute constructor. Requesting a live attribute through the instance-oriented API constructs an object and can therefore execute constructor code. The lesson makes the difference visible with a counter: metadata inspection leaves it unchanged, while instance construction increments it. An annotation is not automatically a validation or authorization action.

A framework can deliberately interpret attributes and enforce policy, but that reader and enforcement path are additional behavior to understand and test. A common wrong answer assumes all attribute APIs are passive or assumes the mere presence of a marker changes the annotated type. For tooling, choose the least effectful API that supplies the required information, filter the intended attribute type explicitly, and define inheritance and multiplicity rules rather than relying on whichever metadata record happens to appear first.

What a strong answer covers

  • Metadata reading does not invoke the custom constructor.
  • Instance retrieval can execute constructor effects.
  • Policy requires a consumer and enforcement path.

Common pitfall

Assuming an attribute declaration enforces its own policy or that every retrieval path is inert.

Follow-up questions

  1. How do named arguments differ from constructor arguments?
  2. When might inherited or multiple attributes need special handling?

Self-review

Choose what to revisit. These marks are your own assessment and are separate from lesson completion.

Primary references

Open lesson and code ↗

← All interview questions