How can I run code in a C# class definition each time any instance of the class is deserialized?
        Posted  
        
            by Ben
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Ben
        
        
        
        Published on 2010-04-26T19:52:24Z
        Indexed on 
            2010/04/26
            19:53 UTC
        
        
        Read the original article
        Hit count: 340
        
I am trying to derive a class from ObservableCollection and I need to run just a single line of code each and every time any instance of this class is deserialized. My thought was to do this:
[Serializable]
public class ObservableCollection2<T> : ObservableCollection<T>, ISerializable
{
    public ObservableCollection2()
        : base()
    { }
    public ObservableCollection2(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
        // Put additional code here.
    }
    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);
    }
}
But I don't have access to those base methods related to serialization. Am I forced to re-write all of the serialization manually?
© Stack Overflow or respective owner