Example #1
0
        void RaiseOnDeserializedEvent(object obj)
        {
            SerializationCallbacks sc = SerializationCallbacks
                                        .GetSerializationCallbacks(obj.GetType());

            sc.RaiseOnDeserialized(obj, _context);
        }
 public SerializationCallbacks(Type type)
 {
     this.onSerializingList   = SerializationCallbacks.GetMethodsByAttribute(type, typeof(OnSerializingAttribute));
     this.onSerializedList    = SerializationCallbacks.GetMethodsByAttribute(type, typeof(OnSerializedAttribute));
     this.onDeserializingList = SerializationCallbacks.GetMethodsByAttribute(type, typeof(OnDeserializingAttribute));
     this.onDeserializedList  = SerializationCallbacks.GetMethodsByAttribute(type, typeof(OnDeserializedAttribute));
 }
        public static SerializationCallbacks GetSerializationCallbacks(Type t)
        {
            SerializationCallbacks serializationCallbacks = (SerializationCallbacks)SerializationCallbacks.cache[t];

            if (serializationCallbacks != null)
            {
                return(serializationCallbacks);
            }
            object obj = SerializationCallbacks.cache_lock;
            SerializationCallbacks result;

            lock (obj)
            {
                serializationCallbacks = (SerializationCallbacks)SerializationCallbacks.cache[t];
                if (serializationCallbacks == null)
                {
                    Hashtable hashtable = (Hashtable)SerializationCallbacks.cache.Clone();
                    serializationCallbacks       = new SerializationCallbacks(t);
                    hashtable[t]                 = serializationCallbacks;
                    SerializationCallbacks.cache = hashtable;
                }
                result = serializationCallbacks;
            }
            return(result);
        }
Example #4
0
 public static SerializationCallbacks GetSerializationCallbacks(Type t)
 {
     lock (cache.SyncRoot) {
         SerializationCallbacks sc = (SerializationCallbacks)cache [t];
         if (sc == null)
         {
             sc        = new SerializationCallbacks(t);
             cache [t] = sc;
         }
         return(sc);
     }
 }
        /// <summary>Registers the object upon which events will be raised.</summary>
        /// <param name="obj">The object to register.</param>
        public void RegisterObject(object obj)
        {
            if (this.seen.Contains(obj))
            {
                return;
            }
            SerializationCallbacks sc = SerializationCallbacks.GetSerializationCallbacks(obj.GetType());

            this.seen[obj] = 1;
            sc.RaiseOnSerializing(obj, this.context);
            if (sc.HasSerializedCallbacks)
            {
                this.callbacks = (SerializationCallbacks.CallbackHandler) Delegate.Combine(this.callbacks, new SerializationCallbacks.CallbackHandler(delegate(StreamingContext ctx)
                {
                    sc.RaiseOnSerialized(obj, ctx);
                }));
            }
        }
        public void RegisterObject(object obj)
        {
            if (seen.Contains(obj))
            {
                return;
            }

            SerializationCallbacks sc = SerializationCallbacks
                                        .GetSerializationCallbacks(obj.GetType());

            seen [obj] = HashHelper.NonNullObject;
            sc.RaiseOnSerializing(obj, context);

            if (sc.HasSerializedCallbacks)
            {
                // record for later invocation
                callbacks += delegate(StreamingContext ctx)
                {
                    sc.RaiseOnSerialized(obj, ctx);
                };
            }
        }
        public static SerializationCallbacks GetSerializationCallbacks(Type t)
        {
            SerializationCallbacks sc;

            if (cache.TryGetValue(t, out sc))
            {
                return(sc);
            }

            // Slow path, new entry, we need to copy
            lock (cache_lock) {
                if (!cache.TryGetValue(t, out sc))
                {
                    var copy = new Dictionary <Type, SerializationCallbacks>(cache);

                    sc      = new SerializationCallbacks(t);
                    copy[t] = sc;
                    cache   = copy;
                }
                return(sc);
            }
        }
Example #8
0
        public static SerializationCallbacks GetSerializationCallbacks(Type t)
        {
            SerializationCallbacks sc = (SerializationCallbacks)cache [t];

            if (sc != null)
            {
                return(sc);
            }

            // Slow path, new entry, we need to copy
            lock (cache_lock){
                sc = (SerializationCallbacks)cache [t];
                if (sc == null)
                {
                    Hashtable copy = (Hashtable)cache.Clone();

                    sc       = new SerializationCallbacks(t);
                    copy [t] = sc;
                    cache    = copy;
                }
                return(sc);
            }
        }
        public static SerializationCallbacks GetSerializationCallbacks(Type t)
        {
            SerializationCallbacks sc;
            if (cache.TryGetValue(t, out sc))
                return sc;

            // Slow path, new entry, we need to copy
            lock (cache_lock) {
                if (!cache.TryGetValue(t, out sc)) {
                    var copy = new Dictionary<Type, SerializationCallbacks>(cache);

                    sc = new SerializationCallbacks(t);
                    copy[t] = sc;
                    cache = copy;
                }
                return sc;
            }
        }
Example #10
0
        public virtual void DoFixups()
        {
            _finalFixup = true;

            try
            {
                if (_registeredObjectsCount < _objectRecords.Count)
                {
                    throw new SerializationException("There are some fixups that refer to objects that have not been registered");
                }


                ObjectRecord last       = _lastObjectRecord;
                bool         firstCicle = true;

                // Solve al pending fixups of all objects

                ObjectRecord record = _objectRecordChain;
                while (record != null)
                {
                    bool ready = !(record.IsUnsolvedObjectReference && firstCicle);
                    if (ready)
                    {
                        ready = record.DoFixups(true, this, true);
                    }
                    if (ready)
                    {
                        ready = record.LoadData(this, _selector, _context);
                    }

                    ObjectRecord next;

                    if (ready)
                    {
                        if (record.OriginalObject is IDeserializationCallback)
                        {
                            _deserializedRecords.Add(record);
                        }

                        SerializationCallbacks sc = SerializationCallbacks
                                                    .GetSerializationCallbacks(record.OriginalObject.GetType());
                        if (sc.HasDeserializedCallbacks)
                        {
                            _onDeserializedCallbackRecords.Add(record);
                        }
                        next = record.Next;
                    }
                    else
                    {
                        // There must be an unresolved IObjectReference instance.
                        // Chain the record at the end so it is solved later

                        if ((record.ObjectInstance is IObjectReference) && !firstCicle)
                        {
                            if (record.Status == ObjectRecordStatus.ReferenceSolvingDelayed)
                            {
                                throw new SerializationException("The object with ID " + record.ObjectID + " could not be resolved");
                            }
                            else
                            {
                                record.Status = ObjectRecordStatus.ReferenceSolvingDelayed;
                            }
                        }

                        if (record != _lastObjectRecord)
                        {
                            next                   = record.Next;
                            record.Next            = null;
                            _lastObjectRecord.Next = record;
                            _lastObjectRecord      = record;
                        }
                        else
                        {
                            next = record;
                        }
                    }

                    if (record == last)
                    {
                        firstCicle = false;
                    }
                    record = next;
                }
            }
            finally
            {
                _finalFixup = false;
            }
        }
Example #11
0
 /// <summary>Performs all the recorded fixups.</summary>
 /// <exception cref="T:System.Runtime.Serialization.SerializationException">A fixup was not successfully completed. </exception>
 public virtual void DoFixups()
 {
     this._finalFixup = true;
     try
     {
         if (this._registeredObjectsCount < this._objectRecords.Count)
         {
             throw new SerializationException("There are some fixups that refer to objects that have not been registered");
         }
         ObjectRecord lastObjectRecord = this._lastObjectRecord;
         bool         flag             = true;
         ObjectRecord objectRecord2;
         for (ObjectRecord objectRecord = this._objectRecordChain; objectRecord != null; objectRecord = objectRecord2)
         {
             bool flag2 = !objectRecord.IsUnsolvedObjectReference || !flag;
             if (flag2)
             {
                 flag2 = objectRecord.DoFixups(true, this, true);
             }
             if (flag2)
             {
                 flag2 = objectRecord.LoadData(this, this._selector, this._context);
             }
             if (flag2)
             {
                 if (objectRecord.OriginalObject is IDeserializationCallback)
                 {
                     this._deserializedRecords.Add(objectRecord);
                 }
                 SerializationCallbacks serializationCallbacks = SerializationCallbacks.GetSerializationCallbacks(objectRecord.OriginalObject.GetType());
                 if (serializationCallbacks.HasDeserializedCallbacks)
                 {
                     this._onDeserializedCallbackRecords.Add(objectRecord);
                 }
                 objectRecord2 = objectRecord.Next;
             }
             else
             {
                 if (objectRecord.ObjectInstance is IObjectReference && !flag)
                 {
                     if (objectRecord.Status == ObjectRecordStatus.ReferenceSolvingDelayed)
                     {
                         throw new SerializationException("The object with ID " + objectRecord.ObjectID + " could not be resolved");
                     }
                     objectRecord.Status = ObjectRecordStatus.ReferenceSolvingDelayed;
                 }
                 if (objectRecord != this._lastObjectRecord)
                 {
                     objectRecord2               = objectRecord.Next;
                     objectRecord.Next           = null;
                     this._lastObjectRecord.Next = objectRecord;
                     this._lastObjectRecord      = objectRecord;
                 }
                 else
                 {
                     objectRecord2 = objectRecord;
                 }
             }
             if (objectRecord == lastObjectRecord)
             {
                 flag = false;
             }
         }
     }
     finally
     {
         this._finalFixup = false;
     }
 }
Example #12
0
        private void RaiseOnDeserializedEvent(object obj)
        {
            SerializationCallbacks serializationCallbacks = SerializationCallbacks.GetSerializationCallbacks(obj.GetType());

            serializationCallbacks.RaiseOnDeserialized(obj, this._context);
        }
Example #13
0
        /// <summary>Invokes the method marked with the <see cref="T:System.Runtime.Serialization.OnDeserializingAttribute" />.</summary>
        /// <param name="obj">The instance of the type that contains the method to be invoked.</param>
        public void RaiseOnDeserializingEvent(object obj)
        {
            SerializationCallbacks serializationCallbacks = SerializationCallbacks.GetSerializationCallbacks(obj.GetType());

            serializationCallbacks.RaiseOnDeserializing(obj, this._context);
        }
Example #14
0
		public static SerializationCallbacks GetSerializationCallbacks (Type t)
		{
			SerializationCallbacks sc = (SerializationCallbacks) cache [t];
			if (sc != null)
				return sc;

			// Slow path, new entry, we need to copy
			lock (cache_lock){
				sc = (SerializationCallbacks)  cache [t];
				if (sc == null) {
					Hashtable copy = (Hashtable) cache.Clone ();
				
					sc = new SerializationCallbacks (t);
					copy [t] = sc;
					cache = copy;
				}
				return sc;
			}
		}
 public void RaiseOnDeserialized(object target, StreamingContext contex)
 {
     SerializationCallbacks.Invoke(this.onDeserializedList, target, contex);
 }
Example #16
0
        public virtual void DoFixups()
        {
            _finalFixup = true;

            try
            {
                if (_registeredObjectsCount < _objectRecords.Count)
                {
                    throw new SerializationException("There are some fixups that refer to objects that have not been registered");
                }


                ObjectRecord last = _lastObjectRecord;

                bool firstCycle          = true;
                bool lastCycle           = false;
                int  unresolvedCount     = 0;                   // Unresolved objects found in the current cycle
                int  lastUnresolvedCount = 0;                   // Unresolved objects before the current cycle

                // Solve al pending fixups of all objects

                ObjectRecord record = _objectRecordChain;
                while (record != null)
                {
                    // We ignore object references in the first cycle
                    bool ready = !(record.IsUnsolvedObjectReference && firstCycle);
                    if (ready)
                    {
                        ready = record.DoFixups(true, this, true);
                    }
                    if (ready)
                    {
                        ready = record.LoadData(this, _selector, _context);
                    }

                    ObjectRecord next;

                    if (ready)
                    {
                        if (record.OriginalObject is IDeserializationCallback)
                        {
                            _deserializedRecords.Add(record);
                        }

                        SerializationCallbacks sc = SerializationCallbacks
                                                    .GetSerializationCallbacks(record.OriginalObject.GetType());
                        if (sc.HasDeserializedCallbacks)
                        {
                            _onDeserializedCallbackRecords.Add(record);
                        }
                        next = record.Next;
                    }
                    else
                    {
                        // There must be an unresolved IObjectReference instance.
                        // Chain the record at the end so it is solved later

                        if ((record.ObjectInstance is IObjectReference) && !firstCycle)
                        {
                            if (record.IsUnsolvedObjectReference && lastCycle)
                            {
                                // No more chances to resolve
                                throw new SerializationException("The object with ID " + record.ObjectID + " could not be resolved");
                            }
                            else
                            {
                                unresolvedCount++;
                            }
                        }

                        if (record != _lastObjectRecord)
                        {
                            next                   = record.Next;
                            record.Next            = null;
                            _lastObjectRecord.Next = record;
                            _lastObjectRecord      = record;
                        }
                        else
                        {
                            next = record;
                        }
                    }

                    if (record == last)
                    {
                        last = _lastObjectRecord;
                        if (firstCycle)
                        {
                            firstCycle = false;
                        }
                        else
                        {
                            if (lastUnresolvedCount == unresolvedCount)
                            {
                                lastCycle = true;
                            }
                        }
                        lastUnresolvedCount = unresolvedCount;
                        unresolvedCount     = 0;
                    }
                    record = next;
                }
            }
            finally
            {
                _finalFixup = false;
            }
        }