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