2002-02-19 Duncan Mak <duncan@ximian.com>
[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 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 //
10 //
11
12 using System;
13 using System.Collections;
14
15 namespace System.Runtime.Serialization
16 {
17         public sealed class SerializationInfo
18         {
19                 Hashtable serialized = new Hashtable ();
20                 string assemblyName; // the assembly being serialized
21                 string fullTypeName; // the type being serialized.
22
23                 [CLSCompliant (false)] IFormatterConverter converter;
24
25                 // Constructor
26                 [CLSCompliant (false)]
27                 public SerializationInfo (Type type, IFormatterConverter converter)
28                 {
29                         if (type == null && converter == null)
30                                 throw new ArgumentNullException ("Null arguments.");
31                         
32                         this.converter = converter;
33                         assemblyName = type.Assembly.FullName;
34                         fullTypeName = type.FullName;
35                 }
36
37                 // Properties
38                 public string AssemblyName
39                 {
40                         get { return assemblyName; }
41                         
42                         set {
43                                 if (value == null)
44                                         throw new ArgumentNullException ("Argument is null.");
45                                 assemblyName = value;
46                         }
47                 }
48                 
49                 public string FullTypeName
50                 {
51                         get { return fullTypeName; }
52                         
53                         set {
54                                 if ( value == null)
55                                         throw new ArgumentNullException ("Argument is null.");
56                                 fullTypeName = value;
57                         }
58                 }
59                 
60                 public int MemberCount
61                 {
62                         get { return serialized.Count; }
63                 }
64
65                 // Methods
66                 public void AddValue (string name, object value, Type type)
67                 {
68                         if (serialized.ContainsKey (name))
69                                 throw new SerializationException ("Value has been serialized already.");
70                         
71                         SerializationEntry values = new SerializationEntry (name, type, value);
72                         serialized.Add (name, values);
73                 }
74
75                 public object GetValue (string name, Type type)
76                 {
77                         if (name == null)
78                                 throw new ArgumentNullException ("name is null.");
79                         if (!serialized.ContainsKey (name))
80                                 throw new SerializationException ("No element named " + name + " could be found.");
81                                                 
82                         SerializationEntry values = (SerializationEntry) serialized [name];
83
84                         if (values.ObjectType != type)
85                                 throw new InvalidCastException ("Invalid Type casting.");
86                         
87                         return values.Value;
88                 }
89
90                 public void SetType (Type type)
91                 {
92                         if (type == null)
93                                 throw new ArgumentNullException ("type is null.");
94
95                         fullTypeName = type.FullName;
96                         assemblyName = type.Assembly.FullName;
97                 }
98
99                 public SerializationInfoEnumerator GetEnumerator ()
100                 {
101                         return new SerializationInfoEnumerator (serialized);
102                 }
103                 
104                 [CLSCompliant(false)]
105                 public void AddValue (string name, short value)
106                 {
107                         AddValue (name, value, typeof (System.Int16));
108                 }
109
110                 [CLSCompliant(false)]
111                 public void AddValue (string name, UInt16 value)
112                 {
113                         AddValue (name, value, typeof (System.UInt16));
114                 }
115                 
116                 public void AddValue (string name, int value)
117                 {
118                         AddValue (name, value, typeof (System.Int32));
119                 }
120                 
121                 public void AddValue (string name, byte value)
122                 {
123                         AddValue (name, value, typeof (System.Byte));
124                 }
125                 
126                 public void AddValue (string name, bool value)
127                 {
128                         AddValue (name, value, typeof (System.Boolean));
129                 }
130                
131                 public void AddValue (string name, char value)
132                 {
133                         AddValue (name, value, typeof (System.Char));
134                 }
135
136                 [CLSCompliant(false)]
137                 public void AddValue (string name, SByte value)
138                 {
139                         AddValue (name, value, typeof (System.SByte));
140                 }
141                 
142                 public void AddValue (string name, double value)
143                 {
144                         AddValue (name, value, typeof (System.Double));
145                 }
146                 
147                 public void AddValue (string name, Decimal value)
148                 {
149                         AddValue (name, value, typeof (System.Decimal));
150                 }
151                 
152                 public void AddValue (string name, DateTime value)
153                 {
154                         AddValue (name, value, typeof (System.DateTime));
155                 }
156                 
157                 public void AddValue (string name, float value)
158                 {
159                         AddValue (name, value, typeof (System.Single));
160                 }
161
162                 [CLSCompliant(false)]
163                 public void AddValue (string name, UInt32 value)
164                 {
165                         AddValue (name, value, typeof (System.UInt32));
166                 }
167                
168                 public void AddValue (string name, long value)
169                 {
170                         AddValue (name, value, typeof (System.Int64));
171                 }
172
173                 [CLSCompliant(false)]
174                 public void AddValue (string name, UInt64 value)
175                 {
176                         AddValue (name, value, typeof (System.UInt64));
177                 }
178                 
179                 public void AddValue (string name, object value)
180                 {
181                         AddValue (name, value, value.GetType ());
182                 }               
183                 
184                 public bool GetBoolean (string name)
185                 {
186                         return (bool) GetValue (name, typeof (System.Boolean)); 
187                 }
188                 
189                 public byte GetByte (string name)
190                 {
191                         return (byte) GetValue (name, typeof (System.Byte));
192                 }
193                 
194                 public char GetChar (string name)
195                 {
196                         return (char) GetValue (name, typeof (System.Char));
197                 }
198
199                 public DateTime GetDateTime (string name)
200                 {
201                         return (DateTime) GetValue (name, typeof (System.DateTime));
202                 }
203                 
204                 public Decimal GetDecimal (string name)
205                 {
206                         return (Decimal) GetValue (name, typeof (System.Decimal));
207                 }
208                 
209                 public double GetDouble (string name)
210                 {
211                         return (double) GetValue (name, typeof (System.Double));
212                 }
213                                                 
214                 public short GetInt16 (string name)
215                 {
216                         return (short) GetValue (name, typeof (System.Int16));
217                 }
218                 
219                 public int GetInt32 (string name)
220                 {
221                         return (int) GetValue (name, typeof (System.Int32));
222                 }
223                
224                 public long GetInt64 (string name)
225                 {
226                         return (long) GetValue (name, typeof (System.Int64));
227                 }
228
229                 [CLSCompliant(false)]
230                 public SByte GetSByte (string name)
231                 {
232                         return (sbyte) GetValue (name, typeof (System.SByte));
233                 }
234                 
235                 public float GetSingle (string name)
236                 {
237                         return (float) GetValue (name, typeof (System.Single));
238                 }
239                 
240                 public string GetString (string name)
241                 {
242                         return (string) GetValue (name, typeof (System.String));
243                 }
244
245                 [CLSCompliant(false)]
246                 public UInt16 GetUInt16 (string name)
247                 {
248                         return (UInt16) GetValue (name, typeof (System.UInt16));
249                 }
250                 
251                 [CLSCompliant(false)]
252                 public UInt32 GetUInt32 (string name)
253                 {
254                         return (UInt32) GetValue (name, typeof (System.UInt32));
255                 }
256                 [CLSCompliant(false)]
257                 public UInt64 GetUInt64 (string name)
258                 {
259                         return (UInt64) GetValue (name, typeof (System.UInt64));
260                 }
261         }
262 }