nonserializedattribute

Benefits of [NonSerialized] when [Serializable] is not used

丶灬走出姿态 提交于 2019-12-23 07:12:15
问题 I'm looking through some existing code in a project I'm working on, and I found a class that is implemented as: public class ThingOne { private int A; private int B; [NonSerialized] private System.Timers.Timer timer1; } Shouldn't it look more like this? [Serializable] public class ThingOne { private int A; private int B; [NonSerialized] private System.Timers.Timer timer1; } Or is there some additional benefit to adding [NonSerialized] even when the class itself is not Serializable? 回答1:

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

ぃ、小莉子 提交于 2019-11-30 04:54:16
I can't find "field" listed as a C# keyword anywhere. Does anyone know the background on this? 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: Attributes that apply to the assembly. Very visible in AssemblyInfo.cs An attribute applied to the return value of a P/Invoke declaration, [return:MarshalAs] Having the attribute apply to the backing variable of a property or event without accessors. Your case. This is necessary, for example, if you are marking an event as non-serializable. It

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

做~自己de王妃 提交于 2019-11-27 01:52:23
问题 I can't find "field" listed as a C# keyword anywhere. Does anyone know the background on this? 回答1: 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: Attributes that apply to the assembly. Very visible in AssemblyInfo.cs An attribute applied to the return value of a P/Invoke declaration, [return:MarshalAs] Having the attribute apply to the backing variable of a property or event without