How to prevent auto implemented properties from being serialized?

后端 未结 7 1497
终归单人心
终归单人心 2020-12-10 10:28

How can I prevent a auto implemented property from being serialized by the binary formatter? The [NonSerialized] attribute can only be used with fields. And the field is hi

7条回答
  •  無奈伤痛
    2020-12-10 10:41

    I'm not sure you can. This MSDN article on SerializableAttribute suggests you implement ISerializable and control the serialisation yourself:

    All the public and private fields in a type that are marked by the SerializableAttribute are serialized by default, unless the type implements the ISerializable interface to override the serialization process.

    Or switch away from an auto-property for that specific field.

提交回复
热议问题