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)]
43 [System
.Runtime
.InteropServices
.ComVisibleAttribute (true)]
45 public abstract class Formatter
: IFormatter
47 protected Formatter ()
51 protected ObjectIDGenerator m_idGenerator
= new ObjectIDGenerator ();
52 protected Queue m_objectQueue
= new Queue ();
54 public abstract SerializationBinder Binder
{
59 public abstract StreamingContext Context
{
64 public abstract ISurrogateSelector SurrogateSelector
{
69 public abstract object Deserialize (Stream serializationStream
);
71 protected virtual object GetNext (out long objID
)
73 if (m_objectQueue
.Count
== 0)
75 // set the out field to 0
80 Object o
= m_objectQueue
.Dequeue ();
82 objID
= m_idGenerator
.HasId (o
, out FirstTime
);
87 protected virtual long Schedule (object obj
)
93 long ID
= m_idGenerator
.GetId (obj
, out FirstTime
);
95 m_objectQueue
.Enqueue (obj
);
100 public abstract void Serialize (Stream serializationStream
, object graph
);
102 protected abstract void WriteArray (object obj
, string name
, Type memberType
);
104 protected abstract void WriteBoolean (bool val
, string name
);
106 protected abstract void WriteByte (byte val
, string name
);
108 protected abstract void WriteChar (char val
, string name
);
110 protected abstract void WriteDateTime (DateTime val
, string name
);
112 protected abstract void WriteDecimal (Decimal val
, string name
);
114 protected abstract void WriteDouble (double val
, string name
);
116 protected abstract void WriteInt16 (short val
, string name
);
118 protected abstract void WriteInt32 (int val
, string name
);
120 protected abstract void WriteInt64 (long val
, string name
);
122 protected virtual void WriteMember (string memberName
, object data
)
125 WriteObjectRef (data
, memberName
, typeof(Object
));
127 Type dataType
= data
.GetType ();
128 if (dataType
.IsArray
)
129 WriteArray (data
, memberName
, dataType
);
130 else if (dataType
== typeof(bool))
131 WriteBoolean ((bool)data
, memberName
);
132 else if (dataType
== typeof(byte))
133 WriteByte ((byte)data
, memberName
);
134 else if (dataType
== typeof(char))
135 WriteChar ((char)data
, memberName
);
136 else if (dataType
== typeof(DateTime
))
137 WriteDateTime ((DateTime
)data
, memberName
);
138 else if (dataType
== typeof(decimal))
139 WriteDecimal ((decimal)data
, memberName
);
140 else if (dataType
== typeof(double))
141 WriteDouble ((double)data
, memberName
);
142 else if (dataType
== typeof(Int16
))
143 WriteInt16 ((Int16
)data
, memberName
);
144 else if (dataType
== typeof(Int32
))
145 WriteInt32 ((Int32
)data
, memberName
);
146 else if (dataType
== typeof(Int64
))
147 WriteInt64 ((Int64
)data
, memberName
);
148 else if (dataType
== typeof(sbyte))
149 WriteSByte ((sbyte)data
, memberName
);
150 else if (dataType
== typeof(float))
151 WriteSingle ((float)data
, memberName
);
152 else if (dataType
== typeof(TimeSpan
))
153 WriteTimeSpan ((TimeSpan
)data
, memberName
);
154 else if (dataType
== typeof(UInt16
))
155 WriteUInt16 ((UInt16
)data
, memberName
);
156 else if (dataType
== typeof(UInt32
))
157 WriteUInt32 ((UInt32
)data
, memberName
);
158 else if (dataType
== typeof(UInt64
))
159 WriteUInt64 ((UInt64
)data
, memberName
);
160 else if (dataType
.IsValueType
)
161 WriteValueType (data
, memberName
, dataType
);
163 WriteObjectRef (data
, memberName
, dataType
);
166 protected abstract void WriteObjectRef (object obj
, string name
, Type memberType
);
169 [CLSCompliant (false)]
170 protected abstract void WriteSByte (sbyte val
, string name
);
173 protected abstract void WriteSingle (float val
, string name
);
175 protected abstract void WriteTimeSpan (TimeSpan val
, string name
);
177 [CLSCompliant (false)]
178 protected abstract void WriteUInt16 (ushort val
, string name
);
180 [CLSCompliant (false)]
181 protected abstract void WriteUInt32 (uint val
, string name
);
183 [CLSCompliant (false)]
184 protected abstract void WriteUInt64 (ulong val
, string name
);
186 protected abstract void WriteValueType (object obj
, string name
, Type memberType
);