none
[OnDeserialized] and OnDeserialization() allows to change ALL fields after deserialization RRS feed

  • Question

  • Hi,

    Is it right that OnDeserialized attribute and OnDeserialization interface method both allows us to modify any field in the object?

    For example,

        [Serializable]
        class Rectangle: IDeserializationCallback
        {
           
            public int len, width;
            [NonSerialized]
            public int area;
    
            public Rectangle()
            {          
                len = 1;
                width = 2;
                area = len*width;
            }
    
            [OnDeserialized]
            public void ondeserialized(StreamingContext s)
            {
                area  = len*width;
                len   = 2;
                width = 5;
                
            }
    
    
            public void OnDeserialization(object sender)
            {  
                 area  = len*width;
                 len   = 6;
                 width = 7;             
            }
        }

    Now after this Rectangle object is deserialized, Area doesn't match with the length and width fields. 

    I just became curious if this is purely a developer design error or we should have some preventive ways to stop these?



    • Edited by KVRagava Tuesday, April 24, 2018 5:01 AM Code mistake
    Tuesday, April 24, 2018 4:59 AM

All replies

  • Hi KVRagava,

    Thank you for posting here.

    For your question, based onmy test, we could change field's value after deserialization. This not a error. There is no MSDN document said that we could not change it.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, April 29, 2018 3:11 PM
    Moderator