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

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

    Most are used at runtime only. A very limited number are used by the compiler, including:

    • [Conditional(...)] - omit method calls per build symbols
    • [Obsolete(...)] - emit a warning/error as build output
    • [Serializable] - gets written as a CLI flag
    • [Extension] - used for extension methods
    • [AttributeUsage] - affects how attributes are applied
    • -

    There are a range of things like [AssemblyVersion], [AssemblyFileVersion] etc that are used by the compiler when creating the assembly file, and things like [InternalsVisibleTo] which affect accessibility.

    Additionally, tools like PostSharp do extra post-compile steps based on attributes.

    There are some other attributes that the compiler may add to generated types/methods (for anon-methods / types, iterator blocks, etc).

提交回复
热议问题