2 // System.Runtime.Serialization.SerializationInfo.cs
5 // Miguel de Icaza (miguel@ximian.com)
6 // Duncan Mak (duncan@ximian.com)
7 // Dietmar Maurer (dietmar@ximian.com)
9 // (C) Ximian, Inc. http://www.ximian.com
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System
.Collections
;
39 namespace System
.Runtime
.Serialization
41 public sealed class SerializationInfo
43 Hashtable serialized
= new Hashtable ();
44 ArrayList values
= new ArrayList ();
46 string assemblyName
; // the assembly being serialized
47 string fullTypeName
; // the type being serialized.
49 IFormatterConverter converter
;
51 /* used by the runtime */
52 private SerializationInfo (Type type
)
54 assemblyName
= type
.Assembly
.FullName
;
55 fullTypeName
= type
.FullName
;
56 converter
= new FormatterConverter ();
59 /* used by the runtime */
60 private SerializationInfo (Type type
, SerializationEntry
[] data
)
62 int len
= data
.Length
;
64 assemblyName
= type
.Assembly
.FullName
;
65 fullTypeName
= type
.FullName
;
66 converter
= new FormatterConverter ();
68 for (int i
= 0; i
< len
; i
++) {
69 serialized
.Add (data
[i
].Name
, data
[i
]);
70 values
.Add (data
[i
]);
75 [CLSCompliant (false)]
76 public SerializationInfo (Type type
, IFormatterConverter converter
)
79 throw new ArgumentNullException ("type", "Null argument");
81 if (converter
== null)
82 throw new ArgumentNullException ("converter", "Null argument");
84 this.converter
= converter
;
85 assemblyName
= type
.Assembly
.FullName
;
86 fullTypeName
= type
.FullName
;
90 public string AssemblyName
92 get { return assemblyName; }
96 throw new ArgumentNullException ("Argument is null.");
101 public string FullTypeName
103 get { return fullTypeName; }
107 throw new ArgumentNullException ("Argument is null.");
108 fullTypeName
= value;
112 public int MemberCount
114 get { return serialized.Count; }
118 public void AddValue (string name
, object value, Type type
)
121 throw new ArgumentNullException ("name is null");
123 throw new ArgumentNullException ("type is null");
125 if (serialized
.ContainsKey (name
))
126 throw new SerializationException ("Value has been serialized already.");
128 SerializationEntry entry
= new SerializationEntry (name
, type
, value);
130 serialized
.Add (name
, entry
);
134 public object GetValue (string name
, Type type
)
137 throw new ArgumentNullException ("name is null.");
139 throw new ArgumentNullException ("type");
140 if (!serialized
.ContainsKey (name
))
141 throw new SerializationException ("No element named " + name
+ " could be found.");
143 SerializationEntry entry
= (SerializationEntry
) serialized
[name
];
145 if (entry
.Value
!= null && !type
.IsAssignableFrom (entry
.Value
.GetType()))
146 return converter
.Convert (entry
.Value
, type
);
151 public void SetType (Type type
)
154 throw new ArgumentNullException ("type is null.");
156 fullTypeName
= type
.FullName
;
157 assemblyName
= type
.Assembly
.FullName
;
160 public SerializationInfoEnumerator
GetEnumerator ()
162 return new SerializationInfoEnumerator (values
);
165 public void AddValue (string name
, short value)
167 AddValue (name
, value, typeof (System
.Int16
));
170 [CLSCompliant(false)]
171 public void AddValue (string name
, UInt16
value)
173 AddValue (name
, value, typeof (System
.UInt16
));
176 public void AddValue (string name
, int value)
178 AddValue (name
, value, typeof (System
.Int32
));
181 public void AddValue (string name
, byte value)
183 AddValue (name
, value, typeof (System
.Byte
));
186 public void AddValue (string name
, bool value)
188 AddValue (name
, value, typeof (System
.Boolean
));
191 public void AddValue (string name
, char value)
193 AddValue (name
, value, typeof (System
.Char
));
196 [CLSCompliant(false)]
197 public void AddValue (string name
, SByte
value)
199 AddValue (name
, value, typeof (System
.SByte
));
202 public void AddValue (string name
, double value)
204 AddValue (name
, value, typeof (System
.Double
));
207 public void AddValue (string name
, Decimal
value)
209 AddValue (name
, value, typeof (System
.Decimal
));
212 public void AddValue (string name
, DateTime
value)
214 AddValue (name
, value, typeof (System
.DateTime
));
217 public void AddValue (string name
, float value)
219 AddValue (name
, value, typeof (System
.Single
));
222 [CLSCompliant(false)]
223 public void AddValue (string name
, UInt32
value)
225 AddValue (name
, value, typeof (System
.UInt32
));
228 public void AddValue (string name
, long value)
230 AddValue (name
, value, typeof (System
.Int64
));
233 [CLSCompliant(false)]
234 public void AddValue (string name
, UInt64
value)
236 AddValue (name
, value, typeof (System
.UInt64
));
239 public void AddValue (string name
, object value)
242 AddValue (name
, value, typeof (System
.Object
));
244 AddValue (name
, value, value.GetType ());
247 public bool GetBoolean (string name
)
249 object value = GetValue (name
, typeof (System
.Boolean
));
250 return converter
.ToBoolean (value);
253 public byte GetByte (string name
)
255 object value = GetValue (name
, typeof (System
.Byte
));
256 return converter
.ToByte (value);
259 public char GetChar (string name
)
261 object value = GetValue (name
, typeof (System
.Char
));
262 return converter
.ToChar (value);
265 public DateTime
GetDateTime (string name
)
267 object value = GetValue (name
, typeof (System
.DateTime
));
268 return converter
.ToDateTime (value);
271 public Decimal
GetDecimal (string name
)
273 object value = GetValue (name
, typeof (System
.Decimal
));
274 return converter
.ToDecimal (value);
277 public double GetDouble (string name
)
279 object value = GetValue (name
, typeof (System
.Double
));
280 return converter
.ToDouble (value);
283 public short GetInt16 (string name
)
285 object value = GetValue (name
, typeof (System
.Int16
));
286 return converter
.ToInt16 (value);
289 public int GetInt32 (string name
)
291 object value = GetValue (name
, typeof (System
.Int32
));
292 return converter
.ToInt32 (value);
295 public long GetInt64 (string name
)
297 object value = GetValue (name
, typeof (System
.Int64
));
298 return converter
.ToInt64 (value);
301 [CLSCompliant(false)]
302 public SByte
GetSByte (string name
)
304 object value = GetValue (name
, typeof (System
.SByte
));
305 return converter
.ToSByte (value);
308 public float GetSingle (string name
)
310 object value = GetValue (name
, typeof (System
.Single
));
311 return converter
.ToSingle (value);
314 public string GetString (string name
)
316 object value = GetValue (name
, typeof (System
.String
));
317 if (value == null) return null;
318 return converter
.ToString (value);
321 [CLSCompliant(false)]
322 public UInt16
GetUInt16 (string name
)
324 object value = GetValue (name
, typeof (System
.UInt16
));
325 return converter
.ToUInt16 (value);
328 [CLSCompliant(false)]
329 public UInt32
GetUInt32 (string name
)
331 object value = GetValue (name
, typeof (System
.UInt32
));
332 return converter
.ToUInt32 (value);
334 [CLSCompliant(false)]
335 public UInt64
GetUInt64 (string name
)
337 object value = GetValue (name
, typeof (System
.UInt64
));
338 return converter
.ToUInt64 (value);
341 /* used by the runtime */
342 private SerializationEntry
[] get_entries ()
344 SerializationEntry
[] res
= new SerializationEntry
[this.MemberCount
];
347 foreach (SerializationEntry e
in this)