SerializationInfo.cs 8.8 KB

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