BinaryFormatter.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // BinaryFormatter.cs
  2. //
  3. // Author:
  4. // Dick Porter ([email protected])
  5. //
  6. // (C) 2002 Ximian, Inc. http://www.ximian.com
  7. using System.Runtime.Serialization.Formatters;
  8. using System.Runtime.Serialization;
  9. using System.Reflection;
  10. using System.Collections;
  11. using System.IO;
  12. using System.Runtime.Remoting.Messaging;
  13. namespace System.Runtime.Serialization.Formatters.Binary {
  14. public sealed class BinaryFormatter : IRemotingFormatter, IFormatter
  15. {
  16. private FormatterAssemblyStyle assembly_format;
  17. private SerializationBinder binder;
  18. private StreamingContext context;
  19. private ISurrogateSelector surrogate_selector;
  20. private FormatterTypeStyle type_format;
  21. public BinaryFormatter()
  22. {
  23. surrogate_selector=null;
  24. context=new StreamingContext(StreamingContextStates.All);
  25. }
  26. public BinaryFormatter(ISurrogateSelector selector, StreamingContext context)
  27. {
  28. surrogate_selector=selector;
  29. this.context=context;
  30. }
  31. public FormatterAssemblyStyle AssemblyFormat
  32. {
  33. get {
  34. return(assembly_format);
  35. }
  36. set {
  37. assembly_format=value;
  38. }
  39. }
  40. public SerializationBinder Binder
  41. {
  42. get {
  43. return(binder);
  44. }
  45. set {
  46. binder=value;
  47. }
  48. }
  49. public StreamingContext Context
  50. {
  51. get {
  52. return(context);
  53. }
  54. set {
  55. context=value;
  56. }
  57. }
  58. public ISurrogateSelector SurrogateSelector
  59. {
  60. get {
  61. return(surrogate_selector);
  62. }
  63. set {
  64. surrogate_selector=value;
  65. }
  66. }
  67. public FormatterTypeStyle TypeFormat
  68. {
  69. get {
  70. return(type_format);
  71. }
  72. set {
  73. type_format=value;
  74. }
  75. }
  76. [MonoTODO]
  77. public object Deserialize(Stream serializationStream)
  78. {
  79. if(serializationStream==null) {
  80. throw new ArgumentNullException("serializationStream is null");
  81. }
  82. if(serializationStream.CanSeek &&
  83. serializationStream.Length==0) {
  84. throw new SerializationException("serializationStream supports seeking, but its length is 0");
  85. }
  86. return(null);
  87. }
  88. [MonoTODO]
  89. public object Deserialize(Stream serializationStream, HeaderHandler handler)
  90. {
  91. if(serializationStream==null) {
  92. throw new ArgumentNullException("serializationStream is null");
  93. }
  94. if(serializationStream.CanSeek &&
  95. serializationStream.Length==0) {
  96. throw new SerializationException("serializationStream supports seeking, but its length is 0");
  97. }
  98. return(null);
  99. }
  100. [MonoTODO]
  101. public object DeserializeMethodResponse(Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallmessage)
  102. {
  103. if(serializationStream==null) {
  104. throw new ArgumentNullException("serializationStream is null");
  105. }
  106. if(serializationStream.CanSeek &&
  107. serializationStream.Length==0) {
  108. throw new SerializationException("serializationStream supports seeking, but its length is 0");
  109. }
  110. return(null);
  111. }
  112. [MonoTODO]
  113. public void Serialize(Stream serializationStream, object graph)
  114. {
  115. if(serializationStream==null) {
  116. throw new ArgumentNullException("serializationStream is null");
  117. }
  118. ISerializable ser = graph as ISerializable;
  119. StreamingContext context = new StreamingContext (StreamingContextStates.Remoting);
  120. object [] oa;
  121. if (ser != null) {
  122. SerializationInfo info = new SerializationInfo (graph.GetType (), new FormatterConverter ());
  123. ser.GetObjectData (info, context);
  124. SerializationInfoEnumerator e = info.GetEnumerator ();
  125. oa = new object [info.MemberCount];
  126. int i = 0;
  127. while (e.MoveNext ()) {
  128. oa [i++] = e.Current;
  129. }
  130. Console.WriteLine ("SERIALIZABLE" + info.MemberCount);
  131. } else {
  132. MemberInfo [] members = FormatterServices.GetSerializableMembers (graph.GetType (), context);
  133. oa = FormatterServices.GetObjectData (graph, members);
  134. Console.WriteLine ("NOT SERIALIZABLE" + oa.Length);
  135. }
  136. foreach (object o in oa) {
  137. Console.WriteLine ("OBJ" + o);
  138. }
  139. throw new NotImplementedException ();
  140. }
  141. [MonoTODO]
  142. public void Serialize(Stream serializationStream, object graph, Header[] headers)
  143. {
  144. if(serializationStream==null) {
  145. throw new ArgumentNullException("serializationStream is null");
  146. }
  147. // fixme: what about headers?
  148. Serialize (serializationStream, graph);
  149. }
  150. }
  151. }