Formatter.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // System.Runtime.Serialization.Formatter.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Ximian, Inc.
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Collections;
  34. using System.IO;
  35. namespace System.Runtime.Serialization
  36. {
  37. [CLSCompliant (false)]
  38. [Serializable]
  39. [System.Runtime.InteropServices.ComVisibleAttribute (true)]
  40. public abstract class Formatter : IFormatter
  41. {
  42. protected Formatter ()
  43. {
  44. }
  45. protected ObjectIDGenerator m_idGenerator = new ObjectIDGenerator ();
  46. protected Queue m_objectQueue = new Queue ();
  47. public abstract SerializationBinder Binder {
  48. get;
  49. set;
  50. }
  51. public abstract StreamingContext Context {
  52. get;
  53. set;
  54. }
  55. public abstract ISurrogateSelector SurrogateSelector {
  56. get;
  57. set;
  58. }
  59. public abstract object Deserialize (Stream serializationStream);
  60. protected virtual object GetNext (out long objID)
  61. {
  62. if (m_objectQueue.Count == 0)
  63. {
  64. // set the out field to 0
  65. objID = 0L;
  66. return null;
  67. }
  68. Object o = m_objectQueue.Dequeue ();
  69. bool FirstTime;
  70. objID = m_idGenerator.HasId (o, out FirstTime);
  71. return o;
  72. }
  73. protected virtual long Schedule (object obj)
  74. {
  75. if (obj == null)
  76. return 0L;
  77. bool FirstTime;
  78. long ID = m_idGenerator.GetId (obj, out FirstTime);
  79. if (FirstTime)
  80. m_objectQueue.Enqueue (obj);
  81. return ID;
  82. }
  83. public abstract void Serialize (Stream serializationStream, object graph);
  84. protected abstract void WriteArray (object obj, string name, Type memberType);
  85. protected abstract void WriteBoolean (bool val, string name);
  86. protected abstract void WriteByte (byte val, string name);
  87. protected abstract void WriteChar (char val, string name);
  88. protected abstract void WriteDateTime (DateTime val, string name);
  89. protected abstract void WriteDecimal (Decimal val, string name);
  90. protected abstract void WriteDouble (double val, string name);
  91. protected abstract void WriteInt16 (short val, string name);
  92. protected abstract void WriteInt32 (int val, string name);
  93. protected abstract void WriteInt64 (long val, string name);
  94. protected virtual void WriteMember (string memberName, object data)
  95. {
  96. if (data == null)
  97. WriteObjectRef (data, memberName, typeof(Object));
  98. Type dataType = data.GetType ();
  99. if (dataType.IsArray)
  100. WriteArray (data, memberName, dataType);
  101. else if (dataType == typeof(bool))
  102. WriteBoolean ((bool)data, memberName);
  103. else if (dataType == typeof(byte))
  104. WriteByte ((byte)data, memberName);
  105. else if (dataType == typeof(char))
  106. WriteChar ((char)data, memberName);
  107. else if (dataType == typeof(DateTime))
  108. WriteDateTime ((DateTime)data, memberName);
  109. else if (dataType == typeof(decimal))
  110. WriteDecimal ((decimal)data, memberName);
  111. else if (dataType == typeof(double))
  112. WriteDouble ((double)data, memberName);
  113. else if (dataType == typeof(Int16))
  114. WriteInt16 ((Int16)data, memberName);
  115. else if (dataType == typeof(Int32))
  116. WriteInt32 ((Int32)data, memberName);
  117. else if (dataType == typeof(Int64))
  118. WriteInt64 ((Int64)data, memberName);
  119. else if (dataType == typeof(sbyte))
  120. WriteSByte ((sbyte)data, memberName);
  121. else if (dataType == typeof(float))
  122. WriteSingle ((float)data, memberName);
  123. else if (dataType == typeof(TimeSpan))
  124. WriteTimeSpan ((TimeSpan)data, memberName);
  125. else if (dataType == typeof(UInt16))
  126. WriteUInt16 ((UInt16)data, memberName);
  127. else if (dataType == typeof(UInt32))
  128. WriteUInt32 ((UInt32)data, memberName);
  129. else if (dataType == typeof(UInt64))
  130. WriteUInt64 ((UInt64)data, memberName);
  131. else if (dataType.IsValueType)
  132. WriteValueType (data, memberName, dataType);
  133. WriteObjectRef (data, memberName, dataType);
  134. }
  135. protected abstract void WriteObjectRef (object obj, string name, Type memberType);
  136. [CLSCompliant (false)]
  137. protected abstract void WriteSByte (sbyte val, string name);
  138. protected abstract void WriteSingle (float val, string name);
  139. protected abstract void WriteTimeSpan (TimeSpan val, string name);
  140. [CLSCompliant (false)]
  141. protected abstract void WriteUInt16 (ushort val, string name);
  142. [CLSCompliant (false)]
  143. protected abstract void WriteUInt32 (uint val, string name);
  144. [CLSCompliant (false)]
  145. protected abstract void WriteUInt64 (ulong val, string name);
  146. protected abstract void WriteValueType (object obj, string name, Type memberType);
  147. }
  148. }