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

  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:  Formatter
  18. **
  19. **
  20. ** Purpose: The abstract base class for all COM+ Runtime
  21. **          Serialization Formatters.
  22. **
  23. **
  24. ===========================================================*/
  25. namespace System.Runtime.Serialization
  26. {
  27.     using System.Threading;
  28.     using System.Runtime.Remoting;
  29.     using System;
  30.     using System.Collections;
  31.     using System.Reflection;
  32.     using System.IO;
  33.     using System.Globalization;
  34.     // This abstract class provides some helper methods for implementing
  35.     // IFormatter. It will manage queueing objects for serialization
  36.     // (the functionality formerly provided by the IGraphWalker interface)
  37.     // and generating ids on a per-object basis.
  38.     [Serializable(), CLSCompliant(false)]
  39.     [System.Runtime.InteropServices.ComVisible(true)]
  40.     public abstract class Formatter : IFormatter
  41.     {
  42.        
  43.         protected ObjectIDGenerator m_idGenerator;
  44.         protected Queue m_objectQueue;
  45.        
  46.         // The default constructor instantiates the queue for objects
  47.         // to be serialized and creates a new instance of the
  48.         // ObjectIDGenerator.
  49.         protected Formatter()
  50.         {
  51.             m_objectQueue = new Queue();
  52.             m_idGenerator = new ObjectIDGenerator();
  53.         }
  54.        
  55.         public abstract object Deserialize(Stream serializationStream);
  56.        
  57.         // This gives back the next object to be serialized. Objects
  58.         // are returned in a FIFO order based on how they were passed
  59.         // to Schedule. The id of the object is put into the objID parameter
  60.         // and the Object itself is returned from the function.
  61.         protected virtual object GetNext(out long objID)
  62.         {
  63.             bool isNew;
  64.            
  65.             if (m_objectQueue.Count == 0) {
  66.                 objID = 0;
  67.                 return null;
  68.             }
  69.            
  70.             object obj = m_objectQueue.Dequeue();
  71.             objID = m_idGenerator.HasId(obj, out isNew);
  72.             if (isNew) {
  73.                 throw new SerializationException(Environment.GetResourceString("Serialization_NoID"));
  74.             }
  75.            
  76.             return obj;
  77.         }
  78.        
  79.         // Schedules an object for later serialization if it hasn't already been scheduled.
  80.         // We get an ID for obj and put it on the queue for later serialization
  81.         // if this is a new object id.
  82.         protected virtual long Schedule(object obj)
  83.         {
  84.             bool isNew;
  85.             long id;
  86.            
  87.             if (obj == null) {
  88.                 return 0;
  89.             }
  90.            
  91.             id = m_idGenerator.GetId(obj, out isNew);
  92.            
  93.             if (isNew) {
  94.                 m_objectQueue.Enqueue(obj);
  95.             }
  96.             return id;
  97.         }
  98.        
  99.        
  100.         public abstract void Serialize(Stream serializationStream, object graph);
  101.        
  102.         // Writes an array to the stream
  103.         protected abstract void WriteArray(object obj, string name, Type memberType);
  104.        
  105.         // Writes a boolean to the stream.
  106.         protected abstract void WriteBoolean(bool val, string name);
  107.        
  108.         // Writes a byte to the stream.
  109.         protected abstract void WriteByte(byte val, string name);
  110.        
  111.         // Writes a character to the stream.
  112.         protected abstract void WriteChar(char val, string name);
  113.        
  114.         // Writes an instance of DateTime to the stream.
  115.         protected abstract void WriteDateTime(DateTime val, string name);
  116.        
  117.         // Writes an instance of Decimal to the stream.
  118.         protected abstract void WriteDecimal(decimal val, string name);
  119.        
  120.         // Writes an instance of Double to the stream.
  121.         protected abstract void WriteDouble(double val, string name);
  122.        
  123.         // Writes an instance of Int16 to the stream.
  124.         protected abstract void WriteInt16(short val, string name);
  125.        
  126.         // Writes an instance of Int32 to the stream.
  127.        
  128.         protected abstract void WriteInt32(int val, string name);
  129.        
  130.         // Writes an instance of Int64 to the stream.
  131.         protected abstract void WriteInt64(long val, string name);
  132.        
  133.         // Writes an object reference to the stream. Schedules the object with the graph walker
  134.         // to handle the work.
  135.         protected abstract void WriteObjectRef(object obj, string name, Type memberType);
  136.        
  137.         // Switches on the type of the member to determine which of the Write* methods
  138.         // to call in order to write this particular member to the stream.
  139.         protected virtual void WriteMember(string memberName, object data)
  140.         {
  141.            
  142.             BCLDebug.Trace("SER", "[Formatter.WriteMember]data: ", data);
  143.            
  144.             if (data == null) {
  145.                 WriteObjectRef(data, memberName, typeof(object));
  146.                 return;
  147.             }
  148.            
  149.             Type varType = data.GetType();
  150.            
  151.             BCLDebug.Trace("SER", "[Formatter.WriteMember]data is of type: ", varType);
  152.            
  153.             if (varType == typeof(bool)) {
  154.                 WriteBoolean(Convert.ToBoolean(data, CultureInfo.InvariantCulture), memberName);
  155.             }
  156.             else if (varType == typeof(char)) {
  157.                 WriteChar(Convert.ToChar(data, CultureInfo.InvariantCulture), memberName);
  158.             }
  159.             else if (varType == typeof(sbyte)) {
  160.                 WriteSByte(Convert.ToSByte(data, CultureInfo.InvariantCulture), memberName);
  161.             }
  162.             else if (varType == typeof(byte)) {
  163.                 WriteByte(Convert.ToByte(data, CultureInfo.InvariantCulture), memberName);
  164.             }
  165.             else if (varType == typeof(Int16)) {
  166.                 WriteInt16(Convert.ToInt16(data, CultureInfo.InvariantCulture), memberName);
  167.             }
  168.             else if (varType == typeof(Int32)) {
  169.                 WriteInt32(Convert.ToInt32(data, CultureInfo.InvariantCulture), memberName);
  170.             }
  171.             else if (varType == typeof(Int64)) {
  172.                 WriteInt64(Convert.ToInt64(data, CultureInfo.InvariantCulture), memberName);
  173.             }
  174.             else if (varType == typeof(float)) {
  175.                 WriteSingle(Convert.ToSingle(data, CultureInfo.InvariantCulture), memberName);
  176.             }
  177.             else if (varType == typeof(double)) {
  178.                 WriteDouble(Convert.ToDouble(data, CultureInfo.InvariantCulture), memberName);
  179.             }
  180.             else if (varType == typeof(DateTime)) {
  181.                 WriteDateTime(Convert.ToDateTime(data, CultureInfo.InvariantCulture), memberName);
  182.             }
  183.             else if (varType == typeof(decimal)) {
  184.                 WriteDecimal(Convert.ToDecimal(data, CultureInfo.InvariantCulture), memberName);
  185.             }
  186.             else if (varType == typeof(UInt16)) {
  187.                 WriteUInt16(Convert.ToUInt16(data, CultureInfo.InvariantCulture), memberName);
  188.             }
  189.             else if (varType == typeof(UInt32)) {
  190.                 WriteUInt32(Convert.ToUInt32(data, CultureInfo.InvariantCulture), memberName);
  191.             }
  192.             else if (varType == typeof(UInt64)) {
  193.                 WriteUInt64(Convert.ToUInt64(data, CultureInfo.InvariantCulture), memberName);
  194.             }
  195.             else {
  196.                 if (varType.IsArray) {
  197.                     WriteArray(data, memberName, varType);
  198.                 }
  199.                 else if (varType.IsValueType) {
  200.                     WriteValueType(data, memberName, varType);
  201.                 }
  202.                 else {
  203.                     WriteObjectRef(data, memberName, varType);
  204.                 }
  205.             }
  206.         }
  207.        
  208.         // Writes an instance of SByte to the stream.
  209.         [CLSCompliant(false)]
  210.         protected abstract void WriteSByte(sbyte val, string name);
  211.        
  212.         // Writes an instance of Single to the stream.
  213.         protected abstract void WriteSingle(float val, string name);
  214.        
  215.         // Writes an instance of TimeSpan to the stream.
  216.         protected abstract void WriteTimeSpan(TimeSpan val, string name);
  217.        
  218.        
  219.         // Writes an instance of an ushort to the stream.
  220.         [CLSCompliant(false)]
  221.         protected abstract void WriteUInt16(ushort val, string name);
  222.        
  223.         // Writes an instance of an uint to the stream.
  224.         [CLSCompliant(false)]
  225.         protected abstract void WriteUInt32(uint val, string name);
  226.        
  227.         // Writes an instance of a ulong to the stream.
  228.         [CLSCompliant(false)]
  229.         protected abstract void WriteUInt64(ulong val, string name);
  230.        
  231.        
  232.         // Writes a valuetype out to the stream.
  233.         protected abstract void WriteValueType(object obj, string name, Type memberType);
  234.        
  235.        
  236.         public abstract ISurrogateSelector SurrogateSelector {
  237.             get;
  238.             set;
  239.         }
  240.        
  241.         public abstract SerializationBinder Binder {
  242.             get;
  243.             set;
  244.         }
  245.        
  246.         public abstract StreamingContext Context {
  247.             get;
  248.             set;
  249.         }
  250.     }
  251.    
  252. }

Developer Fusion