问题
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