NonSerialized attribute on a constant

∥☆過路亽.° 提交于 2021-02-08 10:30:48

问题


Browsing .NET Reference source for some details on ClaimsIdentity class I noticed this:

[NonSerialized]
const string PreFix = "System.Security.ClaimsIdentity.";
[NonSerialized]
const string ActorKey = PreFix + "actor"; 

What might be a possible reason to use NonSerializedAttribute on a const?


回答1:


const values are serializable unless they have the NonSerializedAttribute attached to them. Whoever wrote that code, decided they didn't want to have those values serialized.

Read the MSDN docs on the SerializableAttribute, specifically:

When you apply the SerializableAttribute attribute to a type, all private and public fields are serialized by default. You can control serialization more granularly by implementing the ISerializable interface to override the serialization process.




回答2:


Const are for whole class and not tied with instance.



来源:https://stackoverflow.com/questions/50443196/nonserialized-attribute-on-a-constant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!