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