2 // System.Runtime.Serialization.Formatter.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System
.Collections
;
38 namespace System
.Runtime
.Serialization
40 [CLSCompliant (false)]
42 [System
.Runtime
.InteropServices
.ComVisibleAttribute (true)]
43 public abstract class Formatter
: IFormatter
45 protected Formatter ()
49 protected ObjectIDGenerator m_idGenerator
= new ObjectIDGenerator ();
50 protected Queue m_objectQueue
= new Queue ();
52 public abstract SerializationBinder Binder
{
57 public abstract StreamingContext Context
{
62 public abstract ISurrogateSelector SurrogateSelector
{
67 public abstract object Deserialize (Stream serializationStream
);
69 protected virtual object GetNext (out long objID
)
71 if (m_objectQueue
.Count
== 0)
73 // set the out field to 0
78 Object o
= m_objectQueue
.Dequeue ();
80 objID
= m_idGenerator
.HasId (o
, out FirstTime
);
85 protected virtual long Schedule (object obj
)
91 long ID
= m_idGenerator
.GetId (obj
, out FirstTime
);
93 m_objectQueue
.Enqueue (obj
);
98 public abstract void Serialize (Stream serializationStream
, object graph
);
100 protected abstract void WriteArray (object obj
, string name
, Type memberType
);
102 protected abstract void WriteBoolean (bool val
, string name
);
104 protected abstract void WriteByte (byte val
, string name
);
106 protected abstract void WriteChar (char val
, string name
);
108 protected abstract void WriteDateTime (DateTime val
, string name
);
110 protected abstract void WriteDecimal (Decimal val
, string name
);
112 protected abstract void WriteDouble (double val
, string name
);
114 protected abstract void WriteInt16 (short val
, string name
);
116 protected abstract void WriteInt32 (int val
, string name
);
118 protected abstract void WriteInt64 (long val
, string name
);
120 protected virtual void WriteMember (string memberName
, object data
)
123 WriteObjectRef (data
, memberName
, typeof(Object
));
125 Type dataType
= data
.GetType ();
126 if (dataType
.IsArray
)
127 WriteArray (data
, memberName
, dataType
);
128 else if (dataType
== typeof(bool))
129 WriteBoolean ((bool)data
, memberName
);
130 else if (dataType
== typeof(byte))
131 WriteByte ((byte)data
, memberName
);
132 else if (dataType
== typeof(char))
133 WriteChar ((char)data
, memberName
);
134 else if (dataType
== typeof(DateTime
))
135 WriteDateTime ((DateTime
)data
, memberName
);
136 else if (dataType
== typeof(decimal))
137 WriteDecimal ((decimal)data
, memberName
);
138 else if (dataType
== typeof(double))
139 WriteDouble ((double)data
, memberName
);
140 else if (dataType
== typeof(Int16
))
141 WriteInt16 ((Int16
)data
, memberName
);
142 else if (dataType
== typeof(Int32
))
143 WriteInt32 ((Int32
)data
, memberName
);
144 else if (dataType
== typeof(Int64
))
145 WriteInt64 ((Int64
)data
, memberName
);
146 else if (dataType
== typeof(sbyte))
147 WriteSByte ((sbyte)data
, memberName
);
148 else if (dataType
== typeof(float))
149 WriteSingle ((float)data
, memberName
);
150 else if (dataType
== typeof(TimeSpan
))
151 WriteTimeSpan ((TimeSpan
)data
, memberName
);
152 else if (dataType
== typeof(UInt16
))
153 WriteUInt16 ((UInt16
)data
, memberName
);
154 else if (dataType
== typeof(UInt32
))
155 WriteUInt32 ((UInt32
)data
, memberName
);
156 else if (dataType
== typeof(UInt64
))
157 WriteUInt64 ((UInt64
)data
, memberName
);
158 else if (dataType
.IsValueType
)
159 WriteValueType (data
, memberName
, dataType
);
161 WriteObjectRef (data
, memberName
, dataType
);
164 protected abstract void WriteObjectRef (object obj
, string name
, Type memberType
);
167 [CLSCompliant (false)]
168 protected abstract void WriteSByte (sbyte val
, string name
);
171 protected abstract void WriteSingle (float val
, string name
);
173 protected abstract void WriteTimeSpan (TimeSpan val
, string name
);
175 [CLSCompliant (false)]
176 protected abstract void WriteUInt16 (ushort val
, string name
);
178 [CLSCompliant (false)]
179 protected abstract void WriteUInt32 (uint val
, string name
);
181 [CLSCompliant (false)]
182 protected abstract void WriteUInt64 (ulong val
, string name
);
184 protected abstract void WriteValueType (object obj
, string name
, Type memberType
);