The Labs \ Source Viewer \ SSCLI \ System.Runtime.Serialization \ SerializationObjectManager

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class: SerializationObjectManager
  18. **
  19. **
  20. ** Purpose: Encapsulates serialization time management, mainly invoking serialization events
  21. **
  22. **
  23. ============================================================*/
  24. namespace System.Runtime.Serialization
  25. {
  26.     using System;
  27.     using System.Collections;
  28.     using System.Runtime.Serialization;
  29.     using System.Security.Permissions;
  30.    
  31.     public sealed class SerializationObjectManager
  32.     {
  33.         private Hashtable m_objectSeenTable = new Hashtable();
  34.         // Table to keep track of objects [OnSerializing] has been called on
  35.         private SerializationEventHandler m_onSerializedHandler;
  36.         private StreamingContext m_context;
  37.        
  38.        
  39.         public SerializationObjectManager(StreamingContext context)
  40.         {
  41.             m_context = context;
  42.             m_objectSeenTable = new Hashtable();
  43.         }
  44.        
  45.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  46.         public void RegisterObject(object obj)
  47.         {
  48.             // Invoke OnSerializing for this object
  49.             SerializationEvents cache = SerializationEventsCache.GetSerializationEventsForType(obj.GetType());
  50.             // Check to make sure type has serializing events
  51.             if (cache.HasOnSerializingEvents) {
  52.                 // Check to see if we have invoked the events on the object
  53.                 if (m_objectSeenTable[obj] == null) {
  54.                     m_objectSeenTable[obj] = true;
  55.                     // Invoke the events
  56.                     cache.InvokeOnSerializing(obj, m_context);
  57.                     // Register for OnSerialized event
  58.                     AddOnSerialized(obj);
  59.                 }
  60.             }
  61.         }
  62.        
  63.         public void RaiseOnSerializedEvent()
  64.         {
  65.             if (m_onSerializedHandler != null) {
  66.                 m_onSerializedHandler(m_context);
  67.             }
  68.         }
  69.        
  70.         private void AddOnSerialized(object obj)
  71.         {
  72.             SerializationEvents cache = SerializationEventsCache.GetSerializationEventsForType(obj.GetType());
  73.             m_onSerializedHandler = cache.AddOnSerialized(obj, m_onSerializedHandler);
  74.         }
  75.        
  76.        
  77.     }
  78.    
  79. }

Developer Fusion