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