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