SerializationInfo.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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.Generic;
  36. namespace System.Runtime.Serialization
  37. {
  38. [System.Runtime.InteropServices.ComVisibleAttribute (true)]
  39. public sealed class SerializationInfo
  40. {
  41. Dictionary<string, SerializationEntry> serialized = new Dictionary<string, SerializationEntry> ();
  42. List<SerializationEntry> values = new List<SerializationEntry> ();
  43. string assemblyName; // the assembly being serialized
  44. string fullTypeName; // the type being serialized.
  45. #if NET_4_0
  46. Type objectType;
  47. bool isAssemblyNameSetExplicit;
  48. bool isFullTypeNameSetExplicit;
  49. #endif
  50. IFormatterConverter converter;
  51. /* used by the runtime */
  52. private SerializationInfo (Type type)
  53. {
  54. assemblyName = type.Assembly.FullName;
  55. fullTypeName = type.FullName;
  56. converter = new FormatterConverter ();
  57. #if NET_4_0
  58. objectType = type;
  59. #endif
  60. }
  61. /* used by the runtime */
  62. private SerializationInfo (Type type, SerializationEntry [] data)
  63. {
  64. int len = data.Length;
  65. assemblyName = type.Assembly.FullName;
  66. fullTypeName = type.FullName;
  67. converter = new FormatterConverter ();
  68. #if NET_4_0
  69. objectType = type;
  70. #endif
  71. for (int i = 0; i < len; i++) {
  72. serialized.Add (data [i].Name, data [i]);
  73. values.Add (data [i]);
  74. }
  75. }
  76. // Constructor
  77. [CLSCompliant (false)]
  78. public SerializationInfo (Type type, IFormatterConverter converter)
  79. {
  80. if (type == null)
  81. throw new ArgumentNullException ("type", "Null argument");
  82. if (converter == null)
  83. throw new ArgumentNullException ("converter", "Null argument");
  84. this.converter = converter;
  85. assemblyName = type.Assembly.FullName;
  86. fullTypeName = type.FullName;
  87. #if NET_4_0
  88. objectType = type;
  89. #endif
  90. }
  91. // Properties
  92. public string AssemblyName
  93. {
  94. get { return assemblyName; }
  95. set {
  96. if (value == null)
  97. throw new ArgumentNullException ("Argument is null.");
  98. assemblyName = value;
  99. #if NET_4_0
  100. isAssemblyNameSetExplicit = true;
  101. #endif
  102. }
  103. }
  104. public string FullTypeName
  105. {
  106. get { return fullTypeName; }
  107. set {
  108. if ( value == null)
  109. throw new ArgumentNullException ("Argument is null.");
  110. fullTypeName = value;
  111. #if NET_4_0
  112. isFullTypeNameSetExplicit = true;
  113. #endif
  114. }
  115. }
  116. public int MemberCount
  117. {
  118. get { return serialized.Count; }
  119. }
  120. #if NET_4_0
  121. public bool IsAssemblyNameSetExplicit {
  122. get {
  123. return isAssemblyNameSetExplicit;
  124. }
  125. }
  126. public bool IsFullTypeNameSetExplicit {
  127. get {
  128. return isFullTypeNameSetExplicit;
  129. }
  130. }
  131. public Type ObjectType {
  132. get {
  133. return objectType;
  134. }
  135. }
  136. #endif
  137. // Methods
  138. public void AddValue (string name, object value, Type type)
  139. {
  140. if (name == null)
  141. throw new ArgumentNullException ("name is null");
  142. if (type == null)
  143. throw new ArgumentNullException ("type is null");
  144. if (serialized.ContainsKey (name))
  145. throw new SerializationException ("Value has been serialized already.");
  146. SerializationEntry entry = new SerializationEntry (name, type, value);
  147. serialized.Add (name, entry);
  148. values.Add (entry);
  149. }
  150. public object GetValue (string name, Type type)
  151. {
  152. if (name == null)
  153. throw new ArgumentNullException ("name is null.");
  154. if (type == null)
  155. throw new ArgumentNullException ("type");
  156. if (!serialized.ContainsKey (name))
  157. throw new SerializationException ("No element named " + name + " could be found.");
  158. SerializationEntry entry = serialized [name];
  159. if (entry.Value != null && !type.IsInstanceOfType (entry.Value))
  160. return converter.Convert (entry.Value, type);
  161. else
  162. return entry.Value;
  163. }
  164. internal bool HasKey (string name)
  165. {
  166. return serialized.ContainsKey (name);
  167. }
  168. public void SetType (Type type)
  169. {
  170. if (type == null)
  171. throw new ArgumentNullException ("type is null.");
  172. fullTypeName = type.FullName;
  173. assemblyName = type.Assembly.FullName;
  174. #if NET_4_0
  175. objectType = type;
  176. isAssemblyNameSetExplicit = false;
  177. isFullTypeNameSetExplicit = false;
  178. #endif
  179. }
  180. public SerializationInfoEnumerator GetEnumerator ()
  181. {
  182. return new SerializationInfoEnumerator (values);
  183. }
  184. public void AddValue (string name, short value)
  185. {
  186. AddValue (name, value, typeof (System.Int16));
  187. }
  188. [CLSCompliant(false)]
  189. public void AddValue (string name, UInt16 value)
  190. {
  191. AddValue (name, value, typeof (System.UInt16));
  192. }
  193. public void AddValue (string name, int value)
  194. {
  195. AddValue (name, value, typeof (System.Int32));
  196. }
  197. public void AddValue (string name, byte value)
  198. {
  199. AddValue (name, value, typeof (System.Byte));
  200. }
  201. public void AddValue (string name, bool value)
  202. {
  203. AddValue (name, value, typeof (System.Boolean));
  204. }
  205. public void AddValue (string name, char value)
  206. {
  207. AddValue (name, value, typeof (System.Char));
  208. }
  209. [CLSCompliant(false)]
  210. public void AddValue (string name, SByte value)
  211. {
  212. AddValue (name, value, typeof (System.SByte));
  213. }
  214. public void AddValue (string name, double value)
  215. {
  216. AddValue (name, value, typeof (System.Double));
  217. }
  218. public void AddValue (string name, Decimal value)
  219. {
  220. AddValue (name, value, typeof (System.Decimal));
  221. }
  222. public void AddValue (string name, DateTime value)
  223. {
  224. AddValue (name, value, typeof (System.DateTime));
  225. }
  226. public void AddValue (string name, float value)
  227. {
  228. AddValue (name, value, typeof (System.Single));
  229. }
  230. [CLSCompliant(false)]
  231. public void AddValue (string name, UInt32 value)
  232. {
  233. AddValue (name, value, typeof (System.UInt32));
  234. }
  235. public void AddValue (string name, long value)
  236. {
  237. AddValue (name, value, typeof (System.Int64));
  238. }
  239. [CLSCompliant(false)]
  240. public void AddValue (string name, UInt64 value)
  241. {
  242. AddValue (name, value, typeof (System.UInt64));
  243. }
  244. public void AddValue (string name, object value)
  245. {
  246. if (value == null)
  247. AddValue (name, value, typeof (System.Object));
  248. else
  249. AddValue (name, value, value.GetType ());
  250. }
  251. public bool GetBoolean (string name)
  252. {
  253. object value = GetValue (name, typeof (System.Boolean));
  254. return converter.ToBoolean (value);
  255. }
  256. public byte GetByte (string name)
  257. {
  258. object value = GetValue (name, typeof (System.Byte));
  259. return converter.ToByte (value);
  260. }
  261. public char GetChar (string name)
  262. {
  263. object value = GetValue (name, typeof (System.Char));
  264. return converter.ToChar (value);
  265. }
  266. public DateTime GetDateTime (string name)
  267. {
  268. object value = GetValue (name, typeof (System.DateTime));
  269. return converter.ToDateTime (value);
  270. }
  271. public Decimal GetDecimal (string name)
  272. {
  273. object value = GetValue (name, typeof (System.Decimal));
  274. return converter.ToDecimal (value);
  275. }
  276. public double GetDouble (string name)
  277. {
  278. object value = GetValue (name, typeof (System.Double));
  279. return converter.ToDouble (value);
  280. }
  281. public short GetInt16 (string name)
  282. {
  283. object value = GetValue (name, typeof (System.Int16));
  284. return converter.ToInt16 (value);
  285. }
  286. public int GetInt32 (string name)
  287. {
  288. object value = GetValue (name, typeof (System.Int32));
  289. return converter.ToInt32 (value);
  290. }
  291. public long GetInt64 (string name)
  292. {
  293. object value = GetValue (name, typeof (System.Int64));
  294. return converter.ToInt64 (value);
  295. }
  296. [CLSCompliant(false)]
  297. public SByte GetSByte (string name)
  298. {
  299. object value = GetValue (name, typeof (System.SByte));
  300. return converter.ToSByte (value);
  301. }
  302. public float GetSingle (string name)
  303. {
  304. object value = GetValue (name, typeof (System.Single));
  305. return converter.ToSingle (value);
  306. }
  307. public string GetString (string name)
  308. {
  309. object value = GetValue (name, typeof (System.String));
  310. if (value == null) return null;
  311. return converter.ToString (value);
  312. }
  313. [CLSCompliant(false)]
  314. public UInt16 GetUInt16 (string name)
  315. {
  316. object value = GetValue (name, typeof (System.UInt16));
  317. return converter.ToUInt16 (value);
  318. }
  319. [CLSCompliant(false)]
  320. public UInt32 GetUInt32 (string name)
  321. {
  322. object value = GetValue (name, typeof (System.UInt32));
  323. return converter.ToUInt32 (value);
  324. }
  325. [CLSCompliant(false)]
  326. public UInt64 GetUInt64 (string name)
  327. {
  328. object value = GetValue (name, typeof (System.UInt64));
  329. return converter.ToUInt64 (value);
  330. }
  331. /* used by the runtime */
  332. #pragma warning disable 169
  333. private SerializationEntry [] get_entries ()
  334. {
  335. SerializationEntry [] res = new SerializationEntry [this.MemberCount];
  336. int i = 0;
  337. foreach (SerializationEntry e in this)
  338. res [i++] = e;
  339. return res;
  340. }
  341. #pragma warning restore 169
  342. }
  343. }