1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
//
// System.Runtime.Serialization.SerializationInfo.cs
//
// Author:
// Miguel de Icaza (miguel@ximian.com)
// Duncan Mak (duncan@ximian.com)
// Dietmar Maurer (dietmar@ximian.com)
//
// (C) Ximian, Inc. http://www.ximian.com
//
//
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
namespace System.Runtime.Serialization
{
#if NET_2_0
[System.Runtime.InteropServices.ComVisibleAttribute (true)]
#endif
public sealed class SerializationInfo
{
Hashtable serialized = new Hashtable ();
ArrayList values = new ArrayList ();
string assemblyName; // the assembly being serialized
string fullTypeName; // the type being serialized.
IFormatterConverter converter;
/* used by the runtime */
private SerializationInfo (Type type)
{
assemblyName = type.Assembly.FullName;
fullTypeName = type.FullName;
converter = new FormatterConverter ();
}
/* used by the runtime */
private SerializationInfo (Type type, SerializationEntry [] data)
{
int len = data.Length;
assemblyName = type.Assembly.FullName;
fullTypeName = type.FullName;
converter = new FormatterConverter ();
for (int i = 0; i < len; i++) {
serialized.Add (data [i].Name, data [i]);
values.Add (data [i]);
}
}
// Constructor
[CLSCompliant (false)]
public SerializationInfo (Type type, IFormatterConverter converter)
{
if (type == null)
throw new ArgumentNullException ("type", "Null argument");
if (converter == null)
throw new ArgumentNullException ("converter", "Null argument");
this.converter = converter;
assemblyName = type.Assembly.FullName;
fullTypeName = type.FullName;
}
// Properties
public string AssemblyName
{
get { return assemblyName; }
set {
if (value == null)
throw new ArgumentNullException ("Argument is null.");
assemblyName = value;
}
}
public string FullTypeName
{
get { return fullTypeName; }
set {
if ( value == null)
throw new ArgumentNullException ("Argument is null.");
fullTypeName = value;
}
}
public int MemberCount
{
get { return serialized.Count; }
}
// Methods
public void AddValue (string name, object value, Type type)
{
if (name == null)
throw new ArgumentNullException ("name is null");
if (type == null)
throw new ArgumentNullException ("type is null");
if (serialized.ContainsKey (name))
throw new SerializationException ("Value has been serialized already.");
SerializationEntry entry = new SerializationEntry (name, type, value);
serialized.Add (name, entry);
values.Add (entry);
}
public object GetValue (string name, Type type)
{
if (name == null)
throw new ArgumentNullException ("name is null.");
if (type == null)
throw new ArgumentNullException ("type");
if (!serialized.ContainsKey (name))
throw new SerializationException ("No element named " + name + " could be found.");
SerializationEntry entry = (SerializationEntry) serialized [name];
if (entry.Value != null && !type.IsInstanceOfType (entry.Value))
return converter.Convert (entry.Value, type);
else
return entry.Value;
}
public void SetType (Type type)
{
if (type == null)
throw new ArgumentNullException ("type is null.");
fullTypeName = type.FullName;
assemblyName = type.Assembly.FullName;
}
public SerializationInfoEnumerator GetEnumerator ()
{
return new SerializationInfoEnumerator (values);
}
public void AddValue (string name, short value)
{
AddValue (name, value, typeof (System.Int16));
}
[CLSCompliant(false)]
public void AddValue (string name, UInt16 value)
{
AddValue (name, value, typeof (System.UInt16));
}
public void AddValue (string name, int value)
{
AddValue (name, value, typeof (System.Int32));
}
public void AddValue (string name, byte value)
{
AddValue (name, value, typeof (System.Byte));
}
public void AddValue (string name, bool value)
{
AddValue (name, value, typeof (System.Boolean));
}
public void AddValue (string name, char value)
{
AddValue (name, value, typeof (System.Char));
}
[CLSCompliant(false)]
public void AddValue (string name, SByte value)
{
AddValue (name, value, typeof (System.SByte));
}
public void AddValue (string name, double value)
{
AddValue (name, value, typeof (System.Double));
}
public void AddValue (string name, Decimal value)
{
AddValue (name, value, typeof (System.Decimal));
}
public void AddValue (string name, DateTime value)
{
AddValue (name, value, typeof (System.DateTime));
}
public void AddValue (string name, float value)
{
AddValue (name, value, typeof (System.Single));
}
[CLSCompliant(false)]
public void AddValue (string name, UInt32 value)
{
AddValue (name, value, typeof (System.UInt32));
}
public void AddValue (string name, long value)
{
AddValue (name, value, typeof (System.Int64));
}
[CLSCompliant(false)]
public void AddValue (string name, UInt64 value)
{
AddValue (name, value, typeof (System.UInt64));
}
public void AddValue (string name, object value)
{
if (value == null)
AddValue (name, value, typeof (System.Object));
else
AddValue (name, value, value.GetType ());
}
public bool GetBoolean (string name)
{
object value = GetValue (name, typeof (System.Boolean));
return converter.ToBoolean (value);
}
public byte GetByte (string name)
{
object value = GetValue (name, typeof (System.Byte));
return converter.ToByte (value);
}
public char GetChar (string name)
{
object value = GetValue (name, typeof (System.Char));
return converter.ToChar (value);
}
public DateTime GetDateTime (string name)
{
object value = GetValue (name, typeof (System.DateTime));
return converter.ToDateTime (value);
}
public Decimal GetDecimal (string name)
{
object value = GetValue (name, typeof (System.Decimal));
return converter.ToDecimal (value);
}
public double GetDouble (string name)
{
object value = GetValue (name, typeof (System.Double));
return converter.ToDouble (value);
}
public short GetInt16 (string name)
{
object value = GetValue (name, typeof (System.Int16));
return converter.ToInt16 (value);
}
public int GetInt32 (string name)
{
object value = GetValue (name, typeof (System.Int32));
return converter.ToInt32 (value);
}
public long GetInt64 (string name)
{
object value = GetValue (name, typeof (System.Int64));
return converter.ToInt64 (value);
}
[CLSCompliant(false)]
public SByte GetSByte (string name)
{
object value = GetValue (name, typeof (System.SByte));
return converter.ToSByte (value);
}
public float GetSingle (string name)
{
object value = GetValue (name, typeof (System.Single));
return converter.ToSingle (value);
}
public string GetString (string name)
{
object value = GetValue (name, typeof (System.String));
if (value == null) return null;
return converter.ToString (value);
}
[CLSCompliant(false)]
public UInt16 GetUInt16 (string name)
{
object value = GetValue (name, typeof (System.UInt16));
return converter.ToUInt16 (value);
}
[CLSCompliant(false)]
public UInt32 GetUInt32 (string name)
{
object value = GetValue (name, typeof (System.UInt32));
return converter.ToUInt32 (value);
}
[CLSCompliant(false)]
public UInt64 GetUInt64 (string name)
{
object value = GetValue (name, typeof (System.UInt64));
return converter.ToUInt64 (value);
}
/* used by the runtime */
#pragma warning disable 169
private SerializationEntry [] get_entries ()
{
SerializationEntry [] res = new SerializationEntry [this.MemberCount];
int i = 0;
foreach (SerializationEntry e in this)
res [i++] = e;
return res;
}
#pragma warning restore 169
}
}
|