Is .Net attribute feature used at compile-time or run-time or both?

前端 未结 4 1736
情歌与酒
情歌与酒 2021-01-04 10:53

In .Net, is the attribute feature used at compile-time or run-time or both? Can you give me some examples?

4条回答
  •  春和景丽
    2021-01-04 11:21

    Attributes are output as metadata to the assembly at compile time. This meta data is then used at runtime via reflection - for example using GetCustomAttributes().

    Some attributes are used by the compiler at compile time, too. For example the compiler looks at the AttributeUsageAttribute to determine if an attribute can be used for a specific object.

提交回复
热议问题