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

前端 未结 4 1742
情歌与酒
情歌与酒 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:39

    The compiler adds what is called metadata to the object that is decorated with an attribute. This metadata, whether created via attributes or otherwise, is all accessible at run-time thru Reflection. Thus, you can decorate with attributes and then read the details when the program is running. However, to say that the metadata is "used" at compile time isn't quite correct, as the compiler doesn't care what metadata there is.

提交回复
热议问题