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