How to prevent auto implemented properties from being serialized?

后端 未结 7 1423
终归单人心
终归单人心 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 11:00

    It´s not supported for auto implemented properties. You have to use a backing field and set the NonSerializedAttribute on it.

    public class ClassWithNonSerializedProperty {
    
      [NonSerialized]
      private object _data;  // Backing field of Property Data that is not serialized
    
      public object Data{
        get { return _data; }
        set { _data = value; }
      }
    }
    

提交回复
热议问题