New test.
[mono.git] / mcs / class / corlib / System.Runtime.Serialization / SerializationInfo.cs
1 //
2 // System.Runtime.Serialization.SerializationInfo.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Duncan Mak (duncan@ximian.com)
7 //   Dietmar Maurer (dietmar@ximian.com)
8 //
9 // (C) Ximian, Inc.  http://www.ximian.com
10 //
11 //
12
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
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:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
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.
34 //
35
36 using System;
37 using System.Collections;
38
39 namespace System.Runtime.Serialization
40 {
41 #if NET_2_0
42         [System.Runtime.InteropServices.ComVisibleAttribute (true)]
43 #endif
44         public sealed class SerializationInfo
45         {
46                 Hashtable serialized = new Hashtable ();
47                 ArrayList values = new ArrayList ();
48
49                 string assemblyName; // the assembly being serialized
50                 string fullTypeName; // the type being serialized.
51
52                 IFormatterConverter converter;
53                 
54                 /* used by the runtime */
55                 private SerializationInfo (Type type)
56                 {
57                         assemblyName = type.Assembly.FullName;
58                         fullTypeName = type.FullName;
59                         converter = new FormatterConverter ();
60                 }
61                 
62                 /* used by the runtime */
63                 private SerializationInfo (Type type, SerializationEntry [] data)
64                 {
65                         int len = data.Length;
66
67                         assemblyName = type.Assembly.FullName;
68                         fullTypeName = type.FullName;
69                         converter = new FormatterConverter ();
70
71                         for (int i = 0; i < len; i++) {
72                                 serialized.Add (data [i].Name, data [i]);
73                                 values.Add (data [i]);
74                         }
75                 }
76
77                 // Constructor
78                 [CLSCompliant (false)]
79                 public SerializationInfo (Type type, IFormatterConverter converter)
80                 {
81                         if (type == null)
82                                 throw new ArgumentNullException ("type", "Null argument");
83
84                         if (converter == null)
85                                 throw new ArgumentNullException ("converter", "Null argument");
86                         
87                         this.converter = converter;
88                         assemblyName = type.Assembly.FullName;
89                         fullTypeName = type.FullName;
90                 }
91
92                 // Properties
93                 public string AssemblyName
94                 {
95                         get { return assemblyName; }
96                         
97                         set {
98                                 if (value == null)
99                                         throw new ArgumentNullException ("Argument is null.");
100                                 assemblyName = value;
101                         }
102                 }
103                 
104                 public string FullTypeName
105                 {
106                         get { return fullTypeName; }
107                         
108                         set {
109                                 if ( value == null)
110                                         throw new ArgumentNullException ("Argument is null.");
111                                 fullTypeName = value;
112                         }
113                 }
114                 
115                 public int MemberCount
116                 {
117                         get { return serialized.Count; }
118                 }
119
120                 // Methods
121                 public void AddValue (string name, object value, Type type)
122                 {
123                         if (name == null)
124                                 throw new ArgumentNullException ("name is null");
125                         if (type == null)
126                                 throw new ArgumentNullException ("type is null");
127                         
128                         if (serialized.ContainsKey (name))
129                                 throw new SerializationException ("Value has been serialized already.");
130                         
131                         SerializationEntry entry = new SerializationEntry (name, type, value);
132
133                         serialized.Add (name, entry);
134                         values.Add (entry);
135                 }
136
137                 public object GetValue (string name, Type type)
138                 {
139                         if (name == null)
140                                 throw new ArgumentNullException ("name is null.");
141                         if (type == null)
142                                 throw new ArgumentNullException ("type");
143                         if (!serialized.ContainsKey (name))
144                                 throw new SerializationException ("No element named " + name + " could be found.");
145                                                 
146                         SerializationEntry entry = (SerializationEntry) serialized [name];
147
148                         if (entry.Value != null && !type.IsInstanceOfType (entry.Value))
149                                 return converter.Convert (entry.Value, type);
150                         else
151                                 return entry.Value;
152                 }
153
154                 public void SetType (Type type)
155                 {
156                         if (type == null)
157                                 throw new ArgumentNullException ("type is null.");
158
159                         fullTypeName = type.FullName;
160                         assemblyName = type.Assembly.FullName;
161                 }
162
163                 public SerializationInfoEnumerator GetEnumerator ()
164                 {
165                         return new SerializationInfoEnumerator (values);
166                 }
167                 
168                 public void AddValue (string name, short value)
169                 {
170                         AddValue (name, value, typeof (System.Int16));
171                 }
172
173                 [CLSCompliant(false)]
174                 public void AddValue (string name, UInt16 value)
175                 {
176                         AddValue (name, value, typeof (System.UInt16));
177                 }
178                 
179                 public void AddValue (string name, int value)
180                 {
181                         AddValue (name, value, typeof (System.Int32));
182                 }
183                 
184                 public void AddValue (string name, byte value)
185                 {
186                         AddValue (name, value, typeof (System.Byte));
187                 }
188                 
189                 public void AddValue (string name, bool value)
190                 {
191                         AddValue (name, value, typeof (System.Boolean));
192                 }
193                
194                 public void AddValue (string name, char value)
195                 {
196                         AddValue (name, value, typeof (System.Char));
197                 }
198
199                 [CLSCompliant(false)]
200                 public void AddValue (string name, SByte value)
201                 {
202                         AddValue (name, value, typeof (System.SByte));
203                 }
204                 
205                 public void AddValue (string name, double value)
206                 {
207                         AddValue (name, value, typeof (System.Double));
208                 }
209                 
210                 public void AddValue (string name, Decimal value)
211                 {
212                         AddValue (name, value, typeof (System.Decimal));
213                 }
214                 
215                 public void AddValue (string name, DateTime value)
216                 {
217                         AddValue (name, value, typeof (System.DateTime));
218                 }
219                 
220                 public void AddValue (string name, float value)
221                 {
222                         AddValue (name, value, typeof (System.Single));
223                 }
224
225                 [CLSCompliant(false)]
226                 public void AddValue (string name, UInt32 value)
227                 {
228                         AddValue (name, value, typeof (System.UInt32));
229                 }
230                
231                 public void AddValue (string name, long value)
232                 {
233                         AddValue (name, value, typeof (System.Int64));
234                 }
235
236                 [CLSCompliant(false)]
237                 public void AddValue (string name, UInt64 value)
238                 {
239                         AddValue (name, value, typeof (System.UInt64));
240                 }
241                 
242                 public void AddValue (string name, object value)
243                 {
244                         if (value == null)
245                                 AddValue (name, value, typeof (System.Object));
246                         else
247                                 AddValue (name, value, value.GetType ());
248                 }               
249                 
250                 public bool GetBoolean (string name)
251                 {
252                         object value = GetValue (name, typeof (System.Boolean));
253                         return converter.ToBoolean (value);
254                 }
255                 
256                 public byte GetByte (string name)
257                 {
258                         object value = GetValue (name, typeof (System.Byte));
259                         return converter.ToByte (value);
260                 }
261                 
262                 public char GetChar (string name)
263                 {
264                         object value = GetValue (name, typeof (System.Char));
265                         return converter.ToChar (value);
266                 }
267
268                 public DateTime GetDateTime (string name)
269                 {
270                         object value = GetValue (name, typeof (System.DateTime));
271                         return converter.ToDateTime (value);
272                 }
273                 
274                 public Decimal GetDecimal (string name)
275                 {
276                         object value = GetValue (name, typeof (System.Decimal));
277                         return converter.ToDecimal (value);
278                 }
279                 
280                 public double GetDouble (string name)
281                 {
282                         object value = GetValue (name, typeof (System.Double));
283                         return converter.ToDouble (value);
284                 }
285                                                 
286                 public short GetInt16 (string name)
287                 {
288                         object value = GetValue (name, typeof (System.Int16));
289                         return converter.ToInt16 (value);
290                 }
291                 
292                 public int GetInt32 (string name)
293                 {
294                         object value = GetValue (name, typeof (System.Int32));
295                         return converter.ToInt32 (value);
296                 }
297                
298                 public long GetInt64 (string name)
299                 {
300                         object value = GetValue (name, typeof (System.Int64));
301                         return converter.ToInt64 (value);
302                 }
303
304                 [CLSCompliant(false)]
305                 public SByte GetSByte (string name)
306                 {
307                         object value = GetValue (name, typeof (System.SByte));
308                         return converter.ToSByte (value);
309                 }
310                 
311                 public float GetSingle (string name)
312                 {
313                         object value = GetValue (name, typeof (System.Single));
314                         return converter.ToSingle (value);
315                 }
316                 
317                 public string GetString (string name)
318                 {
319                         object value = GetValue (name, typeof (System.String));
320                         if (value == null) return null;
321                         return converter.ToString (value);
322                 }
323
324                 [CLSCompliant(false)]
325                 public UInt16 GetUInt16 (string name)
326                 {
327                         object value = GetValue (name, typeof (System.UInt16));
328                         return converter.ToUInt16 (value);
329                 }
330                 
331                 [CLSCompliant(false)]
332                 public UInt32 GetUInt32 (string name)
333                 {
334                         object value = GetValue (name, typeof (System.UInt32));
335                         return converter.ToUInt32 (value);
336                 }
337                 [CLSCompliant(false)]
338                 public UInt64 GetUInt64 (string name)
339                 {
340                         object value = GetValue (name, typeof (System.UInt64));
341                         return converter.ToUInt64 (value);
342                 }
343
344                 /* used by the runtime */
345                 private SerializationEntry [] get_entries ()
346                 {
347                         SerializationEntry [] res = new SerializationEntry [this.MemberCount];
348                         int i = 0;
349                         
350                         foreach (SerializationEntry e in this)
351                                 res [i++] = e;
352                         
353                         return res;
354                 }
355         }
356 }