Why do I need “field:” in my attribute declaration “[field:NonSerialized]”?

前端 未结 4 1335
别那么骄傲
别那么骄傲 2020-12-09 04:21

I can\'t find \"field\" listed as a C# keyword anywhere. Does anyone know the background on this?

4条回答
  •  甜味超标
    2020-12-09 04:45

    The C# compiler usually has no trouble figuring out what part of a declaration the attribute applies to. I can think of three cases where you might use it:

    1. Attributes that apply to the assembly. Very visible in AssemblyInfo.cs
    2. An attribute applied to the return value of a P/Invoke declaration, [return:MarshalAs]
    3. Having the attribute apply to the backing variable of a property or event without accessors. Your case.

提交回复
热议问题