SerializationInfo.cs 5.9 KB

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