Change auto implemented properties to normal and deserialization with BinaryFormatter

后端 未结 2 904
不知归路
不知归路 2021-01-13 08:32

I have an object with a property implemented like

public String Bla {get;set;} 

After changing the implementation to something lik

相关标签:
2条回答
  • 2021-01-13 09:01

    Try implementing ISerializable

        [SecurityCritical]
        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new ArgumentNullException("info");
    
            info.AddValue("name of compiler generated field", _bla);
        }
    
    0 讨论(0)
  • 2021-01-13 09:17

    The BinaryFormatter serializes the fields, not the properties.

    You could possibly get it to work by seeing what the auto-generated field name was in ILSpy or something similar and naming yours that way.

    Otherwise as stated by Henrik you would have to write your own deserialization, see this question for more information

    You can probably inspect the deserialization info by implementing ISerializable and special case this field.

    0 讨论(0)
提交回复
热议问题