How to prevent auto implemented properties from being serialized?

后端 未结 7 1425
终归单人心
终归单人心 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; }
      }
    }
    
    0 讨论(0)
提交回复
热议问题