2 // System.Runtime.Serialization.SerializationInfo.cs
5 // Miguel de Icaza (miguel@ximian.com)
6 // Duncan Mak (duncan@ximian.com)
8 // (C) Ximian, Inc. http://www.ximian.com
13 using System.Collections;
15 namespace System.Runtime.Serialization
17 public sealed class SerializationInfo
19 Hashtable serialized = new Hashtable ();
20 string assemblyName; // the assembly being serialized
21 string fullTypeName; // the type being serialized.
23 [CLSCompliant (false)] IFormatterConverter converter;
26 [CLSCompliant (false)]
27 public SerializationInfo (Type type, IFormatterConverter converter)
29 if (type == null && converter == null)
30 throw new ArgumentNullException ("Null arguments.");
32 this.converter = converter;
33 assemblyName = type.Assembly.FullName;
34 fullTypeName = type.FullName;
38 public string AssemblyName
40 get { return assemblyName; }
44 throw new ArgumentNullException ("Argument is null.");
49 public string FullTypeName
51 get { return fullTypeName; }
55 throw new ArgumentNullException ("Argument is null.");
60 public int MemberCount
62 get { return serialized.Count; }
66 public void AddValue (string name, object value, Type type)
68 if (serialized.ContainsKey (name))
69 throw new SerializationException ("Value has been serialized already.");
71 SerializationEntry values = new SerializationEntry (name, type, value);
72 serialized.Add (name, values);
75 public object GetValue (string name, Type type)
78 throw new ArgumentNullException ("name is null.");
79 if (!serialized.ContainsKey (name))
80 throw new SerializationException ("No element named " + name + " could be found.");
82 SerializationEntry values = (SerializationEntry) serialized [name];
84 if (values.ObjectType != type)
85 throw new InvalidCastException ("Invalid Type casting.");
90 public void SetType (Type type)
93 throw new ArgumentNullException ("type is null.");
95 fullTypeName = type.FullName;
96 assemblyName = type.Assembly.FullName;
99 public SerializationInfoEnumerator GetEnumerator ()
101 return new SerializationInfoEnumerator (serialized);
104 [CLSCompliant(false)]
105 public void AddValue (string name, short value)
107 AddValue (name, value, typeof (System.Int16));
110 [CLSCompliant(false)]
111 public void AddValue (string name, UInt16 value)
113 AddValue (name, value, typeof (System.UInt16));
116 public void AddValue (string name, int value)
118 AddValue (name, value, typeof (System.Int32));
121 public void AddValue (string name, byte value)
123 AddValue (name, value, typeof (System.Byte));
126 public void AddValue (string name, bool value)
128 AddValue (name, value, typeof (System.Boolean));
131 public void AddValue (string name, char value)
133 AddValue (name, value, typeof (System.Char));
136 [CLSCompliant(false)]
137 public void AddValue (string name, SByte value)
139 AddValue (name, value, typeof (System.SByte));
142 public void AddValue (string name, double value)
144 AddValue (name, value, typeof (System.Double));
147 public void AddValue (string name, Decimal value)
149 AddValue (name, value, typeof (System.Decimal));
152 public void AddValue (string name, DateTime value)
154 AddValue (name, value, typeof (System.DateTime));
157 public void AddValue (string name, float value)
159 AddValue (name, value, typeof (System.Single));
162 [CLSCompliant(false)]
163 public void AddValue (string name, UInt32 value)
165 AddValue (name, value, typeof (System.UInt32));
168 public void AddValue (string name, long value)
170 AddValue (name, value, typeof (System.Int64));
173 [CLSCompliant(false)]
174 public void AddValue (string name, UInt64 value)
176 AddValue (name, value, typeof (System.UInt64));
179 public void AddValue (string name, object value)
181 AddValue (name, value, value.GetType ());
184 public bool GetBoolean (string name)
186 return (bool) GetValue (name, typeof (System.Boolean));
189 public byte GetByte (string name)
191 return (byte) GetValue (name, typeof (System.Byte));
194 public char GetChar (string name)
196 return (char) GetValue (name, typeof (System.Char));
199 public DateTime GetDateTime (string name)
201 return (DateTime) GetValue (name, typeof (System.DateTime));
204 public Decimal GetDecimal (string name)
206 return (Decimal) GetValue (name, typeof (System.Decimal));
209 public double GetDouble (string name)
211 return (double) GetValue (name, typeof (System.Double));
214 public short GetInt16 (string name)
216 return (short) GetValue (name, typeof (System.Int16));
219 public int GetInt32 (string name)
221 return (int) GetValue (name, typeof (System.Int32));
224 public long GetInt64 (string name)
226 return (long) GetValue (name, typeof (System.Int64));
229 [CLSCompliant(false)]
230 public SByte GetSByte (string name)
232 return (sbyte) GetValue (name, typeof (System.SByte));
235 public float GetSingle (string name)
237 return (float) GetValue (name, typeof (System.Single));
240 public string GetString (string name)
242 return (string) GetValue (name, typeof (System.String));
245 [CLSCompliant(false)]
246 public UInt16 GetUInt16 (string name)
248 return (UInt16) GetValue (name, typeof (System.UInt16));
251 [CLSCompliant(false)]
252 public UInt32 GetUInt32 (string name)
254 return (UInt32) GetValue (name, typeof (System.UInt32));
256 [CLSCompliant(false)]
257 public UInt64 GetUInt64 (string name)
259 return (UInt64) GetValue (name, typeof (System.UInt64));