Formatter.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. public abstract class Formatter : IFormatter
  40. {
  41. protected Formatter ()
  42. {
  43. }
  44. protected ObjectIDGenerator m_idGenerator = new ObjectIDGenerator ();
  45. protected Queue m_objectQueue = new Queue ();
  46. public abstract SerializationBinder Binder {
  47. get;
  48. set;
  49. }
  50. public abstract StreamingContext Context {
  51. get;
  52. set;
  53. }
  54. public abstract ISurrogateSelector SurrogateSelector {
  55. get;
  56. set;
  57. }
  58. public abstract object Deserialize (Stream serializationStream);
  59. protected virtual object GetNext (out long objID)
  60. {
  61. if (m_objectQueue.Count == 0)
  62. {
  63. // set the out field to 0
  64. objID = 0L;
  65. return null;
  66. }
  67. Object o = m_objectQueue.Dequeue ();
  68. bool FirstTime;
  69. objID = m_idGenerator.HasId (o, out FirstTime);
  70. return o;
  71. }
  72. protected virtual long Schedule (object obj)
  73. {
  74. if (obj == null)
  75. return 0L;
  76. bool FirstTime;
  77. long ID = m_idGenerator.GetId (obj, out FirstTime);
  78. if (FirstTime)
  79. m_objectQueue.Enqueue (obj);
  80. return ID;
  81. }
  82. public abstract void Serialize (Stream serializationStream, object graph);
  83. protected abstract void WriteArray (object obj, string name, Type memberType);
  84. protected abstract void WriteBoolean (bool val, string name);
  85. protected abstract void WriteByte (byte val, string name);
  86. protected abstract void WriteChar (char val, string name);
  87. protected abstract void WriteDateTime (DateTime val, string name);
  88. protected abstract void WriteDecimal (Decimal val, string name);
  89. protected abstract void WriteDouble (double val, string name);
  90. protected abstract void WriteInt16 (short val, string name);
  91. protected abstract void WriteInt32 (int val, string name);
  92. protected abstract void WriteInt64 (long val, string name);
  93. protected virtual void WriteMember (string memberName, object data)
  94. {
  95. if (data == null)
  96. WriteObjectRef (data, memberName, typeof(Object));
  97. Type dataType = data.GetType ();
  98. if (dataType.IsArray)
  99. WriteArray (data, memberName, dataType);
  100. else if (dataType == typeof(bool))
  101. WriteBoolean ((bool)data, memberName);
  102. else if (dataType == typeof(byte))
  103. WriteByte ((byte)data, memberName);
  104. else if (dataType == typeof(char))
  105. WriteChar ((char)data, memberName);
  106. else if (dataType == typeof(DateTime))
  107. WriteDateTime ((DateTime)data, memberName);
  108. else if (dataType == typeof(decimal))
  109. WriteDecimal ((decimal)data, memberName);
  110. else if (dataType == typeof(double))
  111. WriteDouble ((double)data, memberName);
  112. else if (dataType == typeof(Int16))
  113. WriteInt16 ((Int16)data, memberName);
  114. else if (dataType == typeof(Int32))
  115. WriteInt32 ((Int32)data, memberName);
  116. else if (dataType == typeof(Int64))
  117. WriteInt64 ((Int64)data, memberName);
  118. else if (dataType == typeof(sbyte))
  119. WriteSByte ((sbyte)data, memberName);
  120. else if (dataType == typeof(float))
  121. WriteSingle ((float)data, memberName);
  122. else if (dataType == typeof(TimeSpan))
  123. WriteTimeSpan ((TimeSpan)data, memberName);
  124. else if (dataType == typeof(UInt16))
  125. WriteUInt16 ((UInt16)data, memberName);
  126. else if (dataType == typeof(UInt32))
  127. WriteUInt32 ((UInt32)data, memberName);
  128. else if (dataType == typeof(UInt64))
  129. WriteUInt64 ((UInt64)data, memberName);
  130. else if (dataType.IsValueType)
  131. WriteValueType (data, memberName, dataType);
  132. WriteObjectRef (data, memberName, dataType);
  133. }
  134. protected abstract void WriteObjectRef (object obj, string name, Type memberType);
  135. [CLSCompliant (false)]
  136. protected abstract void WriteSByte (sbyte val, string name);
  137. protected abstract void WriteSingle (float val, string name);
  138. protected abstract void WriteTimeSpan (TimeSpan val, string name);
  139. [CLSCompliant (false)]
  140. protected abstract void WriteUInt16 (ushort val, string name);
  141. [CLSCompliant (false)]
  142. protected abstract void WriteUInt32 (uint val, string name);
  143. [CLSCompliant (false)]
  144. protected abstract void WriteUInt64 (ulong val, string name);
  145. protected abstract void WriteValueType (object obj, string name, Type memberType);
  146. }
  147. }