Serializer.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. 
  2. using ProtoBuf.Meta;
  3. using System;
  4. using System.IO;
  5. #if !NO_GENERICS
  6. using System.Collections.Generic;
  7. #endif
  8. #if FEAT_IKVM
  9. using Type = IKVM.Reflection.Type;
  10. using IKVM.Reflection;
  11. #else
  12. using System.Reflection;
  13. #endif
  14. namespace ProtoBuf
  15. {
  16. /// <summary>
  17. /// Provides protocol-buffer serialization capability for concrete, attributed types. This
  18. /// is a *default* model, but custom serializer models are also supported.
  19. /// </summary>
  20. /// <remarks>
  21. /// Protocol-buffer serialization is a compact binary format, designed to take
  22. /// advantage of sparse data and knowledge of specific data types; it is also
  23. /// extensible, allowing a type to be deserialized / merged even if some data is
  24. /// not recognised.
  25. /// </remarks>
  26. public
  27. #if FX11
  28. sealed
  29. #else
  30. static
  31. #endif
  32. class Serializer
  33. {
  34. #if FX11
  35. private Serializer() { } // not a static class for C# 1.2 reasons
  36. #endif
  37. #if !NO_RUNTIME && !NO_GENERICS
  38. /// <summary>
  39. /// Suggest a .proto definition for the given type
  40. /// </summary>
  41. /// <typeparam name="T">The type to generate a .proto definition for</typeparam>
  42. /// <returns>The .proto definition as a string</returns>
  43. public static string GetProto<T>()
  44. {
  45. return RuntimeTypeModel.Default.GetSchema(RuntimeTypeModel.Default.MapType(typeof(T)));
  46. }
  47. /// <summary>
  48. /// Create a deep clone of the supplied instance; any sub-items are also cloned.
  49. /// </summary>
  50. public static T DeepClone<T>(T instance)
  51. {
  52. return instance == null ? instance : (T)RuntimeTypeModel.Default.DeepClone(instance);
  53. }
  54. /// <summary>
  55. /// Applies a protocol-buffer stream to an existing instance.
  56. /// </summary>
  57. /// <typeparam name="T">The type being merged.</typeparam>
  58. /// <param name="instance">The existing instance to be modified (can be null).</param>
  59. /// <param name="source">The binary stream to apply to the instance (cannot be null).</param>
  60. /// <returns>The updated instance; this may be different to the instance argument if
  61. /// either the original instance was null, or the stream defines a known sub-type of the
  62. /// original instance.</returns>
  63. public static T Merge<T>(Stream source, T instance)
  64. {
  65. return (T)RuntimeTypeModel.Default.Deserialize(source, instance, typeof(T));
  66. }
  67. /// <summary>
  68. /// Creates a new instance from a protocol-buffer stream
  69. /// </summary>
  70. /// <typeparam name="T">The type to be created.</typeparam>
  71. /// <param name="source">The binary stream to apply to the new instance (cannot be null).</param>
  72. /// <returns>A new, initialized instance.</returns>
  73. public static T Deserialize<T>(Stream source)
  74. {
  75. return (T) RuntimeTypeModel.Default.Deserialize(source, null, typeof(T));
  76. }
  77. /// <summary>
  78. /// Writes a protocol-buffer representation of the given instance to the supplied stream.
  79. /// </summary>
  80. /// <param name="instance">The existing instance to be serialized (cannot be null).</param>
  81. /// <param name="destination">The destination stream to write to.</param>
  82. public static void Serialize<T>(Stream destination, T instance)
  83. {
  84. if(instance != null) {
  85. RuntimeTypeModel.Default.Serialize(destination, instance);
  86. }
  87. }
  88. /// <summary>
  89. /// Serializes a given instance and deserializes it as a different type;
  90. /// this can be used to translate between wire-compatible objects (where
  91. /// two .NET types represent the same data), or to promote/demote a type
  92. /// through an inheritance hierarchy.
  93. /// </summary>
  94. /// <remarks>No assumption of compatibility is made between the types.</remarks>
  95. /// <typeparam name="TFrom">The type of the object being copied.</typeparam>
  96. /// <typeparam name="TTo">The type of the new object to be created.</typeparam>
  97. /// <param name="instance">The existing instance to use as a template.</param>
  98. /// <returns>A new instane of type TNewType, with the data from TOldType.</returns>
  99. public static TTo ChangeType<TFrom,TTo>(TFrom instance)
  100. {
  101. using (MemoryStream ms = new MemoryStream())
  102. {
  103. Serialize<TFrom>(ms, instance);
  104. ms.Position = 0;
  105. return Deserialize<TTo>(ms);
  106. }
  107. }
  108. #if PLAT_BINARYFORMATTER && !(WINRT || PHONE8)
  109. /// <summary>
  110. /// Writes a protocol-buffer representation of the given instance to the supplied SerializationInfo.
  111. /// </summary>
  112. /// <typeparam name="T">The type being serialized.</typeparam>
  113. /// <param name="instance">The existing instance to be serialized (cannot be null).</param>
  114. /// <param name="info">The destination SerializationInfo to write to.</param>
  115. public static void Serialize<T>(System.Runtime.Serialization.SerializationInfo info, T instance) where T : class, System.Runtime.Serialization.ISerializable
  116. {
  117. Serialize<T>(info, new System.Runtime.Serialization.StreamingContext(System.Runtime.Serialization.StreamingContextStates.Persistence), instance);
  118. }
  119. /// <summary>
  120. /// Writes a protocol-buffer representation of the given instance to the supplied SerializationInfo.
  121. /// </summary>
  122. /// <typeparam name="T">The type being serialized.</typeparam>
  123. /// <param name="instance">The existing instance to be serialized (cannot be null).</param>
  124. /// <param name="info">The destination SerializationInfo to write to.</param>
  125. /// <param name="context">Additional information about this serialization operation.</param>
  126. public static void Serialize<T>(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context, T instance) where T : class, System.Runtime.Serialization.ISerializable
  127. {
  128. // note: also tried byte[]... it doesn't perform hugely well with either (compared to regular serialization)
  129. if (info == null) throw new ArgumentNullException("info");
  130. if (instance == null) throw new ArgumentNullException("instance");
  131. if (instance.GetType() != typeof(T)) throw new ArgumentException("Incorrect type", "instance");
  132. using (MemoryStream ms = new MemoryStream())
  133. {
  134. RuntimeTypeModel.Default.Serialize(ms, instance, context);
  135. info.AddValue(ProtoBinaryField, ms.ToArray());
  136. }
  137. }
  138. #endif
  139. #if PLAT_XMLSERIALIZER
  140. /// <summary>
  141. /// Writes a protocol-buffer representation of the given instance to the supplied XmlWriter.
  142. /// </summary>
  143. /// <typeparam name="T">The type being serialized.</typeparam>
  144. /// <param name="instance">The existing instance to be serialized (cannot be null).</param>
  145. /// <param name="writer">The destination XmlWriter to write to.</param>
  146. public static void Serialize<T>(System.Xml.XmlWriter writer, T instance) where T : System.Xml.Serialization.IXmlSerializable
  147. {
  148. if (writer == null) throw new ArgumentNullException("writer");
  149. if (instance == null) throw new ArgumentNullException("instance");
  150. using (MemoryStream ms = new MemoryStream())
  151. {
  152. Serializer.Serialize(ms, instance);
  153. writer.WriteBase64(ms.GetBuffer(), 0, (int)ms.Length);
  154. }
  155. }
  156. /// <summary>
  157. /// Applies a protocol-buffer from an XmlReader to an existing instance.
  158. /// </summary>
  159. /// <typeparam name="T">The type being merged.</typeparam>
  160. /// <param name="instance">The existing instance to be modified (cannot be null).</param>
  161. /// <param name="reader">The XmlReader containing the data to apply to the instance (cannot be null).</param>
  162. public static void Merge<T>(System.Xml.XmlReader reader, T instance) where T : System.Xml.Serialization.IXmlSerializable
  163. {
  164. if (reader == null) throw new ArgumentNullException("reader");
  165. if (instance == null) throw new ArgumentNullException("instance");
  166. const int LEN = 4096;
  167. byte[] buffer = new byte[LEN];
  168. int read;
  169. using (MemoryStream ms = new MemoryStream())
  170. {
  171. int depth = reader.Depth;
  172. while(reader.Read() && reader.Depth > depth)
  173. {
  174. if (reader.NodeType == System.Xml.XmlNodeType.Text)
  175. {
  176. while ((read = reader.ReadContentAsBase64(buffer, 0, LEN)) > 0)
  177. {
  178. ms.Write(buffer, 0, read);
  179. }
  180. if (reader.Depth <= depth) break;
  181. }
  182. }
  183. ms.Position = 0;
  184. Serializer.Merge(ms, instance);
  185. }
  186. }
  187. #endif
  188. private const string ProtoBinaryField = "proto";
  189. #if PLAT_BINARYFORMATTER && !NO_GENERICS && !(WINRT || PHONE8)
  190. /// <summary>
  191. /// Applies a protocol-buffer from a SerializationInfo to an existing instance.
  192. /// </summary>
  193. /// <typeparam name="T">The type being merged.</typeparam>
  194. /// <param name="instance">The existing instance to be modified (cannot be null).</param>
  195. /// <param name="info">The SerializationInfo containing the data to apply to the instance (cannot be null).</param>
  196. public static void Merge<T>(System.Runtime.Serialization.SerializationInfo info, T instance) where T : class, System.Runtime.Serialization.ISerializable
  197. {
  198. Merge<T>(info, new System.Runtime.Serialization.StreamingContext(System.Runtime.Serialization.StreamingContextStates.Persistence), instance);
  199. }
  200. /// <summary>
  201. /// Applies a protocol-buffer from a SerializationInfo to an existing instance.
  202. /// </summary>
  203. /// <typeparam name="T">The type being merged.</typeparam>
  204. /// <param name="instance">The existing instance to be modified (cannot be null).</param>
  205. /// <param name="info">The SerializationInfo containing the data to apply to the instance (cannot be null).</param>
  206. /// <param name="context">Additional information about this serialization operation.</param>
  207. public static void Merge<T>(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context, T instance) where T : class, System.Runtime.Serialization.ISerializable
  208. {
  209. // note: also tried byte[]... it doesn't perform hugely well with either (compared to regular serialization)
  210. if (info == null) throw new ArgumentNullException("info");
  211. if (instance == null) throw new ArgumentNullException("instance");
  212. if (instance.GetType() != typeof(T)) throw new ArgumentException("Incorrect type", "instance");
  213. byte[] buffer = (byte[])info.GetValue(ProtoBinaryField, typeof(byte[]));
  214. using (MemoryStream ms = new MemoryStream(buffer))
  215. {
  216. T result = (T)RuntimeTypeModel.Default.Deserialize(ms, instance, typeof(T), context);
  217. if (!ReferenceEquals(result, instance))
  218. {
  219. throw new ProtoException("Deserialization changed the instance; cannot succeed.");
  220. }
  221. }
  222. }
  223. #endif
  224. #if !NO_GENERICS
  225. /// <summary>
  226. /// Precompiles the serializer for a given type.
  227. /// </summary>
  228. public static void PrepareSerializer<T>()
  229. {
  230. #if FEAT_COMPILER
  231. RuntimeTypeModel model = RuntimeTypeModel.Default;
  232. model[model.MapType(typeof(T))].CompileInPlace();
  233. #endif
  234. }
  235. #if PLAT_BINARYFORMATTER && !(WINRT || PHONE8)
  236. /// <summary>
  237. /// Creates a new IFormatter that uses protocol-buffer [de]serialization.
  238. /// </summary>
  239. /// <typeparam name="T">The type of object to be [de]deserialized by the formatter.</typeparam>
  240. /// <returns>A new IFormatter to be used during [de]serialization.</returns>
  241. public static System.Runtime.Serialization.IFormatter CreateFormatter<T>()
  242. {
  243. #if FEAT_IKVM
  244. throw new NotSupportedException();
  245. #else
  246. return RuntimeTypeModel.Default.CreateFormatter(typeof(T));
  247. #endif
  248. }
  249. #endif
  250. /// <summary>
  251. /// Reads a sequence of consecutive length-prefixed items from a stream, using
  252. /// either base-128 or fixed-length prefixes. Base-128 prefixes with a tag
  253. /// are directly comparable to serializing multiple items in succession
  254. /// (use the <see cref="ListItemTag"/> tag to emulate the implicit behavior
  255. /// when serializing a list/array). When a tag is
  256. /// specified, any records with different tags are silently omitted. The
  257. /// tag is ignored. The tag is ignores for fixed-length prefixes.
  258. /// </summary>
  259. /// <typeparam name="T">The type of object to deserialize.</typeparam>
  260. /// <param name="source">The binary stream containing the serialized records.</param>
  261. /// <param name="style">The prefix style used in the data.</param>
  262. /// <param name="fieldNumber">The tag of records to return (if non-positive, then no tag is
  263. /// expected and all records are returned).</param>
  264. /// <returns>The sequence of deserialized objects.</returns>
  265. public static IEnumerable<T> DeserializeItems<T>(Stream source, PrefixStyle style, int fieldNumber)
  266. {
  267. return RuntimeTypeModel.Default.DeserializeItems<T>(source, style, fieldNumber);
  268. }
  269. /// <summary>
  270. /// Creates a new instance from a protocol-buffer stream that has a length-prefix
  271. /// on data (to assist with network IO).
  272. /// </summary>
  273. /// <typeparam name="T">The type to be created.</typeparam>
  274. /// <param name="source">The binary stream to apply to the new instance (cannot be null).</param>
  275. /// <param name="style">How to encode the length prefix.</param>
  276. /// <returns>A new, initialized instance.</returns>
  277. public static T DeserializeWithLengthPrefix<T>(Stream source, PrefixStyle style)
  278. {
  279. return DeserializeWithLengthPrefix<T>(source, style, 0);
  280. }
  281. /// <summary>
  282. /// Creates a new instance from a protocol-buffer stream that has a length-prefix
  283. /// on data (to assist with network IO).
  284. /// </summary>
  285. /// <typeparam name="T">The type to be created.</typeparam>
  286. /// <param name="source">The binary stream to apply to the new instance (cannot be null).</param>
  287. /// <param name="style">How to encode the length prefix.</param>
  288. /// <param name="fieldNumber">The expected tag of the item (only used with base-128 prefix style).</param>
  289. /// <returns>A new, initialized instance.</returns>
  290. public static T DeserializeWithLengthPrefix<T>(Stream source, PrefixStyle style, int fieldNumber)
  291. {
  292. RuntimeTypeModel model = RuntimeTypeModel.Default;
  293. return (T)model.DeserializeWithLengthPrefix(source, null, model.MapType(typeof(T)), style, fieldNumber);
  294. }
  295. /// <summary>
  296. /// Applies a protocol-buffer stream to an existing instance, using length-prefixed
  297. /// data - useful with network IO.
  298. /// </summary>
  299. /// <typeparam name="T">The type being merged.</typeparam>
  300. /// <param name="instance">The existing instance to be modified (can be null).</param>
  301. /// <param name="source">The binary stream to apply to the instance (cannot be null).</param>
  302. /// <param name="style">How to encode the length prefix.</param>
  303. /// <returns>The updated instance; this may be different to the instance argument if
  304. /// either the original instance was null, or the stream defines a known sub-type of the
  305. /// original instance.</returns>
  306. public static T MergeWithLengthPrefix<T>(Stream source, T instance, PrefixStyle style)
  307. {
  308. RuntimeTypeModel model = RuntimeTypeModel.Default;
  309. return (T)model.DeserializeWithLengthPrefix(source, instance, model.MapType(typeof(T)), style, 0);
  310. }
  311. /// <summary>
  312. /// Writes a protocol-buffer representation of the given instance to the supplied stream,
  313. /// with a length-prefix. This is useful for socket programming,
  314. /// as DeserializeWithLengthPrefix/MergeWithLengthPrefix can be used to read the single object back
  315. /// from an ongoing stream.
  316. /// </summary>
  317. /// <typeparam name="T">The type being serialized.</typeparam>
  318. /// <param name="instance">The existing instance to be serialized (cannot be null).</param>
  319. /// <param name="style">How to encode the length prefix.</param>
  320. /// <param name="destination">The destination stream to write to.</param>
  321. public static void SerializeWithLengthPrefix<T>(Stream destination, T instance, PrefixStyle style)
  322. {
  323. SerializeWithLengthPrefix<T>(destination, instance, style, 0);
  324. }
  325. /// <summary>
  326. /// Writes a protocol-buffer representation of the given instance to the supplied stream,
  327. /// with a length-prefix. This is useful for socket programming,
  328. /// as DeserializeWithLengthPrefix/MergeWithLengthPrefix can be used to read the single object back
  329. /// from an ongoing stream.
  330. /// </summary>
  331. /// <typeparam name="T">The type being serialized.</typeparam>
  332. /// <param name="instance">The existing instance to be serialized (cannot be null).</param>
  333. /// <param name="style">How to encode the length prefix.</param>
  334. /// <param name="destination">The destination stream to write to.</param>
  335. /// <param name="fieldNumber">The tag used as a prefix to each record (only used with base-128 style prefixes).</param>
  336. public static void SerializeWithLengthPrefix<T>(Stream destination, T instance, PrefixStyle style, int fieldNumber)
  337. {
  338. RuntimeTypeModel model = RuntimeTypeModel.Default;
  339. model.SerializeWithLengthPrefix(destination, instance, model.MapType(typeof(T)), style, fieldNumber);
  340. }
  341. #endif
  342. /// <summary>Indicates the number of bytes expected for the next message.</summary>
  343. /// <param name="source">The stream containing the data to investigate for a length.</param>
  344. /// <param name="style">The algorithm used to encode the length.</param>
  345. /// <param name="length">The length of the message, if it could be identified.</param>
  346. /// <returns>True if a length could be obtained, false otherwise.</returns>
  347. public static bool TryReadLengthPrefix(Stream source, PrefixStyle style, out int length)
  348. {
  349. int fieldNumber, bytesRead;
  350. length = ProtoReader.ReadLengthPrefix(source, false, style, out fieldNumber, out bytesRead);
  351. return bytesRead > 0;
  352. }
  353. /// <summary>Indicates the number of bytes expected for the next message.</summary>
  354. /// <param name="buffer">The buffer containing the data to investigate for a length.</param>
  355. /// <param name="index">The offset of the first byte to read from the buffer.</param>
  356. /// <param name="count">The number of bytes to read from the buffer.</param>
  357. /// <param name="style">The algorithm used to encode the length.</param>
  358. /// <param name="length">The length of the message, if it could be identified.</param>
  359. /// <returns>True if a length could be obtained, false otherwise.</returns>
  360. public static bool TryReadLengthPrefix(byte[] buffer, int index, int count, PrefixStyle style, out int length)
  361. {
  362. using (Stream source = new MemoryStream(buffer, index, count))
  363. {
  364. return TryReadLengthPrefix(source, style, out length);
  365. }
  366. }
  367. #endif
  368. /// <summary>
  369. /// The field number that is used as a default when serializing/deserializing a list of objects.
  370. /// The data is treated as repeated message with field number 1.
  371. /// </summary>
  372. public const int ListItemTag = 1;
  373. #if !NO_RUNTIME
  374. /// <summary>
  375. /// Provides non-generic access to the default serializer.
  376. /// </summary>
  377. public
  378. #if FX11
  379. sealed
  380. #else
  381. static
  382. #endif
  383. class NonGeneric
  384. {
  385. #if FX11
  386. private NonGeneric() { } // not a static class for C# 1.2 reasons
  387. #endif
  388. /// <summary>
  389. /// Create a deep clone of the supplied instance; any sub-items are also cloned.
  390. /// </summary>
  391. public static object DeepClone(object instance)
  392. {
  393. return instance == null ? null : RuntimeTypeModel.Default.DeepClone(instance);
  394. }
  395. /// <summary>
  396. /// Writes a protocol-buffer representation of the given instance to the supplied stream.
  397. /// </summary>
  398. /// <param name="instance">The existing instance to be serialized (cannot be null).</param>
  399. /// <param name="dest">The destination stream to write to.</param>
  400. public static void Serialize(Stream dest, object instance)
  401. {
  402. if (instance != null)
  403. {
  404. RuntimeTypeModel.Default.Serialize(dest, instance);
  405. }
  406. }
  407. /// <summary>
  408. /// Creates a new instance from a protocol-buffer stream
  409. /// </summary>
  410. /// <param name="type">The type to be created.</param>
  411. /// <param name="source">The binary stream to apply to the new instance (cannot be null).</param>
  412. /// <returns>A new, initialized instance.</returns>
  413. public static object Deserialize(System.Type type, Stream source)
  414. {
  415. return RuntimeTypeModel.Default.Deserialize(source, null, type);
  416. }
  417. /// <summary>Applies a protocol-buffer stream to an existing instance.</summary>
  418. /// <param name="instance">The existing instance to be modified (cannot be null).</param>
  419. /// <param name="source">The binary stream to apply to the instance (cannot be null).</param>
  420. /// <returns>The updated instance</returns>
  421. public static object Merge(Stream source, object instance)
  422. {
  423. if (instance == null) throw new ArgumentNullException("instance");
  424. return RuntimeTypeModel.Default.Deserialize(source, instance, instance.GetType(), null);
  425. }
  426. /// <summary>
  427. /// Writes a protocol-buffer representation of the given instance to the supplied stream,
  428. /// with a length-prefix. This is useful for socket programming,
  429. /// as DeserializeWithLengthPrefix/MergeWithLengthPrefix can be used to read the single object back
  430. /// from an ongoing stream.
  431. /// </summary>
  432. /// <param name="instance">The existing instance to be serialized (cannot be null).</param>
  433. /// <param name="style">How to encode the length prefix.</param>
  434. /// <param name="destination">The destination stream to write to.</param>
  435. /// <param name="fieldNumber">The tag used as a prefix to each record (only used with base-128 style prefixes).</param>
  436. public static void SerializeWithLengthPrefix(Stream destination, object instance, PrefixStyle style, int fieldNumber)
  437. {
  438. if (instance == null) throw new ArgumentNullException("instance");
  439. RuntimeTypeModel model = RuntimeTypeModel.Default;
  440. model.SerializeWithLengthPrefix(destination, instance, model.MapType(instance.GetType()), style, fieldNumber);
  441. }
  442. /// <summary>
  443. /// Applies a protocol-buffer stream to an existing instance (or null), using length-prefixed
  444. /// data - useful with network IO.
  445. /// </summary>
  446. /// <param name="value">The existing instance to be modified (can be null).</param>
  447. /// <param name="source">The binary stream to apply to the instance (cannot be null).</param>
  448. /// <param name="style">How to encode the length prefix.</param>
  449. /// <param name="resolver">Used to resolve types on a per-field basis.</param>
  450. /// <returns>The updated instance; this may be different to the instance argument if
  451. /// either the original instance was null, or the stream defines a known sub-type of the
  452. /// original instance.</returns>
  453. public static bool TryDeserializeWithLengthPrefix(Stream source, PrefixStyle style, TypeResolver resolver, out object value)
  454. {
  455. value = RuntimeTypeModel.Default.DeserializeWithLengthPrefix(source, null, null, style, 0, resolver);
  456. return value != null;
  457. }
  458. /// <summary>
  459. /// Indicates whether the supplied type is explicitly modelled by the model
  460. /// </summary>
  461. public static bool CanSerialize(Type type)
  462. {
  463. return RuntimeTypeModel.Default.IsDefined(type);
  464. }
  465. }
  466. /// <summary>
  467. /// Global switches that change the behavior of protobuf-net
  468. /// </summary>
  469. public
  470. #if FX11
  471. sealed
  472. #else
  473. static
  474. #endif
  475. class GlobalOptions
  476. {
  477. #if FX11
  478. private GlobalOptions() { } // not a static class for C# 1.2 reasons
  479. #endif
  480. /// <summary>
  481. /// <see cref="RuntimeTypeModel.InferTagFromNameDefault"/>
  482. /// </summary>
  483. [Obsolete("Please use RuntimeTypeModel.Default.InferTagFromNameDefault instead (or on a per-model basis)", false)]
  484. public static bool InferTagFromName
  485. {
  486. get { return RuntimeTypeModel.Default.InferTagFromNameDefault; }
  487. set { RuntimeTypeModel.Default.InferTagFromNameDefault = value; }
  488. }
  489. }
  490. #endif
  491. /// <summary>
  492. /// Maps a field-number to a type
  493. /// </summary>
  494. public delegate Type TypeResolver(int fieldNumber);
  495. /// <summary>
  496. /// Releases any internal buffers that have been reserved for efficiency; this does not affect any serialization
  497. /// operations; simply: it can be used (optionally) to release the buffers for garbage collection (at the expense
  498. /// of having to re-allocate a new buffer for the next operation, rather than re-use prior buffers).
  499. /// </summary>
  500. public static void FlushPool()
  501. {
  502. BufferPool.Flush();
  503. }
  504. }
  505. }