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
42 [System.Runtime.InteropServices.ComVisibleAttribute (true)]
44 public sealed class SerializationInfo
46 Hashtable serialized = new Hashtable ();
47 ArrayList values = new ArrayList ();
49 string assemblyName; // the assembly being serialized
50 string fullTypeName; // the type being serialized.
52 IFormatterConverter converter;
54 /* used by the runtime */
55 private SerializationInfo (Type type)
57 assemblyName = type.Assembly.FullName;
58 fullTypeName = type.FullName;
59 converter = new FormatterConverter ();
62 /* used by the runtime */
63 private SerializationInfo (Type type, SerializationEntry [] data)
65 int len = data.Length;
67 assemblyName = type.Assembly.FullName;
68 fullTypeName = type.FullName;
69 converter = new FormatterConverter ();
71 for (int i = 0; i < len; i++) {
72 serialized.Add (data [i].Name, data [i]);
73 values.Add (data [i]);
78 [CLSCompliant (false)]
79 public SerializationInfo (Type type, IFormatterConverter converter)
82 throw new ArgumentNullException ("type", "Null argument");
84 if (converter == null)
85 throw new ArgumentNullException ("converter", "Null argument");
87 this.converter = converter;
88 assemblyName = type.Assembly.FullName;
89 fullTypeName = type.FullName;
93 public string AssemblyName
95 get { return assemblyName; }
99 throw new ArgumentNullException ("Argument is null.");
100 assemblyName = value;
104 public string FullTypeName
106 get { return fullTypeName; }
110 throw new ArgumentNullException ("Argument is null.");
111 fullTypeName = value;
115 public int MemberCount
117 get { return serialized.Count; }
121 public void AddValue (string name, object value, Type type)
124 throw new ArgumentNullException ("name is null");
126 throw new ArgumentNullException ("type is null");
128 if (serialized.ContainsKey (name))
129 throw new SerializationException ("Value has been serialized already.");
131 SerializationEntry entry = new SerializationEntry (name, type, value);
133 serialized.Add (name, entry);
137 public object GetValue (string name, Type type)
140 throw new ArgumentNullException ("name is null.");
142 throw new ArgumentNullException ("type");
143 if (!serialized.ContainsKey (name))
144 throw new SerializationException ("No element named " + name + " could be found.");
146 SerializationEntry entry = (SerializationEntry) serialized [name];
148 if (entry.Value != null && !type.IsInstanceOfType (entry.Value))
149 return converter.Convert (entry.Value, type);
154 public void SetType (Type type)
157 throw new ArgumentNullException ("type is null.");
159 fullTypeName = type.FullName;
160 assemblyName = type.Assembly.FullName;
163 public SerializationInfoEnumerator GetEnumerator ()
165 return new SerializationInfoEnumerator (values);
168 public void AddValue (string name, short value)
170 AddValue (name, value, typeof (System.Int16));
173 [CLSCompliant(false)]
174 public void AddValue (string name, UInt16 value)
176 AddValue (name, value, typeof (System.UInt16));
179 public void AddValue (string name, int value)
181 AddValue (name, value, typeof (System.Int32));
184 public void AddValue (string name, byte value)
186 AddValue (name, value, typeof (System.Byte));
189 public void AddValue (string name, bool value)
191 AddValue (name, value, typeof (System.Boolean));
194 public void AddValue (string name, char value)
196 AddValue (name, value, typeof (System.Char));
199 [CLSCompliant(false)]
200 public void AddValue (string name, SByte value)
202 AddValue (name, value, typeof (System.SByte));
205 public void AddValue (string name, double value)
207 AddValue (name, value, typeof (System.Double));
210 public void AddValue (string name, Decimal value)
212 AddValue (name, value, typeof (System.Decimal));
215 public void AddValue (string name, DateTime value)
217 AddValue (name, value, typeof (System.DateTime));
220 public void AddValue (string name, float value)
222 AddValue (name, value, typeof (System.Single));
225 [CLSCompliant(false)]
226 public void AddValue (string name, UInt32 value)
228 AddValue (name, value, typeof (System.UInt32));
231 public void AddValue (string name, long value)
233 AddValue (name, value, typeof (System.Int64));
236 [CLSCompliant(false)]
237 public void AddValue (string name, UInt64 value)
239 AddValue (name, value, typeof (System.UInt64));
242 public void AddValue (string name, object value)
245 AddValue (name, value, typeof (System.Object));
247 AddValue (name, value, value.GetType ());
250 public bool GetBoolean (string name)
252 object value = GetValue (name, typeof (System.Boolean));
253 return converter.ToBoolean (value);
256 public byte GetByte (string name)
258 object value = GetValue (name, typeof (System.Byte));
259 return converter.ToByte (value);
262 public char GetChar (string name)
264 object value = GetValue (name, typeof (System.Char));
265 return converter.ToChar (value);
268 public DateTime GetDateTime (string name)
270 object value = GetValue (name, typeof (System.DateTime));
271 return converter.ToDateTime (value);
274 public Decimal GetDecimal (string name)
276 object value = GetValue (name, typeof (System.Decimal));
277 return converter.ToDecimal (value);
280 public double GetDouble (string name)
282 object value = GetValue (name, typeof (System.Double));
283 return converter.ToDouble (value);
286 public short GetInt16 (string name)
288 object value = GetValue (name, typeof (System.Int16));
289 return converter.ToInt16 (value);
292 public int GetInt32 (string name)
294 object value = GetValue (name, typeof (System.Int32));
295 return converter.ToInt32 (value);
298 public long GetInt64 (string name)
300 object value = GetValue (name, typeof (System.Int64));
301 return converter.ToInt64 (value);
304 [CLSCompliant(false)]
305 public SByte GetSByte (string name)
307 object value = GetValue (name, typeof (System.SByte));
308 return converter.ToSByte (value);
311 public float GetSingle (string name)
313 object value = GetValue (name, typeof (System.Single));
314 return converter.ToSingle (value);
317 public string GetString (string name)
319 object value = GetValue (name, typeof (System.String));
320 if (value == null) return null;
321 return converter.ToString (value);
324 [CLSCompliant(false)]
325 public UInt16 GetUInt16 (string name)
327 object value = GetValue (name, typeof (System.UInt16));
328 return converter.ToUInt16 (value);
331 [CLSCompliant(false)]
332 public UInt32 GetUInt32 (string name)
334 object value = GetValue (name, typeof (System.UInt32));
335 return converter.ToUInt32 (value);
337 [CLSCompliant(false)]
338 public UInt64 GetUInt64 (string name)
340 object value = GetValue (name, typeof (System.UInt64));
341 return converter.ToUInt64 (value);
344 /* used by the runtime */
345 private SerializationEntry [] get_entries ()
347 SerializationEntry [] res = new SerializationEntry [this.MemberCount];
350 foreach (SerializationEntry e in this)