When are Attribute Objects created?

后端 未结 3 1588
一向
一向 2021-02-19 20:42

Since Attributes are really just Metadata attached to assemblies, does that mean that Attribute Objects are only created on request (such as when you call GetCustomAttributes)?

3条回答
  •  后悔当初
    2021-02-19 21:08

    From CLR via C#, third edition:

    If you want to construct an attribute object, you must call either GetCustomAttributes or GetCustomAttribute. Every time one of these methods is called, it constructs new instances of the specified attribute type and sets each of the instance’s fields and properties based on the values specified in the source code. These methods return references to fully constructed instances of the applied attribute classes.

    So yes, they are only created on request.

提交回复
热议问题