SerializationInfo.cs 7.4 KB

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