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

前端 未结 4 1330
别那么骄傲
别那么骄傲 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:59

    This is meant to allow you to set NonSerialized attribute on fields, this is useful in serializing events.

    For instance this would give you a compilation error

    [NonSerialized]
    public event SomeEventHandler SomeEvent;
    

    To fix this you have to use field:

    [field:NonSerialized]
    public event SomeEventHandler SomeEvent;
    

    More on this here -- Delegates and Serialization

提交回复
热议问题