Serializer.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. using ProtoBuf.Meta;
  2. using System;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. namespace ProtoBuf
  7. {
  8. /// <summary>
  9. /// Provides protocol-buffer serialization capability for concrete, attributed types. This
  10. /// is a *default* model, but custom serializer models are also supported.
  11. /// </summary>
  12. /// <remarks>
  13. /// Protocol-buffer serialization is a compact binary format, designed to take
  14. /// advantage of sparse data and knowledge of specific data types; it is also
  15. /// extensible, allowing a type to be deserialized / merged even if some data is
  16. /// not recognised.
  17. /// </remarks>
  18. public static class Serializer
  19. {
  20. #if !NO_RUNTIME
  21. /// <summary>
  22. /// Suggest a .proto definition for the given type
  23. /// </summary>
  24. /// <typeparam name="T">The type to generate a .proto definition for</typeparam>
  25. /// <returns>The .proto definition as a string</returns>
  26. public static string GetProto<T>() => GetProto<T>(ProtoSyntax.Proto2);
  27. /// <summary>
  28. /// Suggest a .proto definition for the given type
  29. /// </summary>
  30. /// <typeparam name="T">The type to generate a .proto definition for</typeparam>
  31. /// <returns>The .proto definition as a string</returns>
  32. public static string GetProto<T>(ProtoSyntax syntax)
  33. {
  34. return RuntimeTypeModel.Default.GetSchema(RuntimeTypeModel.Default.MapType(typeof(T)), syntax);
  35. }
  36. /// <summary>
  37. /// Create a deep clone of the supplied instance; any sub-items are also cloned.
  38. /// </summary>
  39. public static T DeepClone<T>(T instance)
  40. {
  41. return instance == null ? instance : (T)RuntimeTypeModel.Default.DeepClone(instance);
  42. }
  43. /// <summary>
  44. /// Applies a protocol-buffer stream to an existing instance.
  45. /// </summary>
  46. /// <typeparam name="T">The type being merged.</typeparam>
  47. /// <param name="instance">The existing instance to be modified (can be null).</param>
  48. /// <param name="source">The binary stream to apply to the instance (cannot be null).</param>
  49. /// <returns>The updated instance; this may be different to the instance argument if
  50. /// either the original instance was null, or the stream defines a known sub-type of the
  51. /// original instance.</returns>
  52. public static T Merge<T>(Stream source, T instance)
  53. {
  54. return (T)RuntimeTypeModel.Default.Deserialize(source, instance, typeof(T));
  55. }
  56. /// <summary>
  57. /// Creates a new instance from a protocol-buffer stream
  58. /// </summary>
  59. /// <typeparam name="T">The type to be created.</typeparam>
  60. /// <param name="source">The binary stream to apply to the new instance (cannot be null).</param>
  61. /// <returns>A new, initialized instance.</returns>
  62. public static T Deserialize<T>(Stream source)
  63. {
  64. return (T)RuntimeTypeModel.Default.Deserialize(source, null, typeof(T));
  65. }
  66. /// <summary>
  67. /// Creates a new instance from a protocol-buffer stream
  68. /// </summary>
  69. /// <param name="type">The type to be created.</param>
  70. /// <param name="source">The binary stream to apply to the new instance (cannot be null).</param>
  71. /// <returns>A new, initialized instance.</returns>
  72. public static object Deserialize(Type type, Stream source)
  73. {
  74. return RuntimeTypeModel.Default.Deserialize(source, null, type);
  75. }
  76. /// <summary>
  77. /// Writes a protocol-buffer representation of the given instance to the supplied stream.
  78. /// </summary>
  79. /// <param name="instance">The existing instance to be serialized (cannot be null).</param>
  80. /// <param name="destination">The destination stream to write to.</param>
  81. public static void Serialize<T>(Stream destination, T instance)
  82. {
  83. if (instance != null)
  84. {
  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 (var ms = new MemoryStream())
  102. {
  103. Serialize<TFrom>(ms, instance);
  104. ms.Position = 0;
  105. return Deserialize<TTo>(ms);
  106. }
  107. }
  108. #if PLAT_BINARYFORMATTER && !(COREFX || PROFILE259)
  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(Helpers.GetBuffer(ms), 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 && !(COREFX || PROFILE259)
  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. /// <summary>
  225. /// Precompiles the serializer for a given type.
  226. /// </summary>
  227. public static void PrepareSerializer<T>()
  228. {
  229. NonGeneric.PrepareSerializer(typeof(T));
  230. }
  231. #if PLAT_BINARYFORMATTER && !(COREFX || PROFILE259)
  232. /// <summary>
  233. /// Creates a new IFormatter that uses protocol-buffer [de]serialization.
  234. /// </summary>
  235. /// <typeparam name="T">The type of object to be [de]deserialized by the formatter.</typeparam>
  236. /// <returns>A new IFormatter to be used during [de]serialization.</returns>
  237. public static System.Runtime.Serialization.IFormatter CreateFormatter<T>()
  238. {
  239. return RuntimeTypeModel.Default.CreateFormatter(typeof(T));
  240. }
  241. #endif
  242. /// <summary>
  243. /// Reads a sequence of consecutive length-prefixed items from a stream, using
  244. /// either base-128 or fixed-length prefixes. Base-128 prefixes with a tag
  245. /// are directly comparable to serializing multiple items in succession
  246. /// (use the <see cref="ListItemTag"/> tag to emulate the implicit behavior
  247. /// when serializing a list/array). When a tag is
  248. /// specified, any records with different tags are silently omitted. The
  249. /// tag is ignored. The tag is ignored for fixed-length prefixes.
  250. /// </summary>
  251. /// <typeparam name="T">The type of object to deserialize.</typeparam>
  252. /// <param name="source">The binary stream containing the serialized records.</param>
  253. /// <param name="style">The prefix style used in the data.</param>
  254. /// <param name="fieldNumber">The tag of records to return (if non-positive, then no tag is
  255. /// expected and all records are returned).</param>
  256. /// <returns>The sequence of deserialized objects.</returns>
  257. public static IEnumerable<T> DeserializeItems<T>(Stream source, PrefixStyle style, int fieldNumber)
  258. {
  259. return RuntimeTypeModel.Default.DeserializeItems<T>(source, style, fieldNumber);
  260. }
  261. /// <summary>
  262. /// Creates a new instance from a protocol-buffer stream that has a length-prefix
  263. /// on data (to assist with network IO).
  264. /// </summary>
  265. /// <typeparam name="T">The type to be created.</typeparam>
  266. /// <param name="source">The binary stream to apply to the new instance (cannot be null).</param>
  267. /// <param name="style">How to encode the length prefix.</param>
  268. /// <returns>A new, initialized instance.</returns>
  269. public static T DeserializeWithLengthPrefix<T>(Stream source, PrefixStyle style)
  270. {
  271. return DeserializeWithLengthPrefix<T>(source, style, 0);
  272. }
  273. /// <summary>
  274. /// Creates a new instance from a protocol-buffer stream that has a length-prefix
  275. /// on data (to assist with network IO).
  276. /// </summary>
  277. /// <typeparam name="T">The type to be created.</typeparam>
  278. /// <param name="source">The binary stream to apply to the new instance (cannot be null).</param>
  279. /// <param name="style">How to encode the length prefix.</param>
  280. /// <param name="fieldNumber">The expected tag of the item (only used with base-128 prefix style).</param>
  281. /// <returns>A new, initialized instance.</returns>
  282. public static T DeserializeWithLengthPrefix<T>(Stream source, PrefixStyle style, int fieldNumber)
  283. {
  284. RuntimeTypeModel model = RuntimeTypeModel.Default;
  285. return (T)model.DeserializeWithLengthPrefix(source, null, model.MapType(typeof(T)), style, fieldNumber);
  286. }
  287. /// <summary>
  288. /// Applies a protocol-buffer stream to an existing instance, using length-prefixed
  289. /// data - useful with network IO.
  290. /// </summary>
  291. /// <typeparam name="T">The type being merged.</typeparam>
  292. /// <param name="instance">The existing instance to be modified (can be null).</param>
  293. /// <param name="source">The binary stream to apply to the instance (cannot be null).</param>
  294. /// <param name="style">How to encode the length prefix.</param>
  295. /// <returns>The updated instance; this may be different to the instance argument if
  296. /// either the original instance was null, or the stream defines a known sub-type of the
  297. /// original instance.</returns>
  298. public static T MergeWithLengthPrefix<T>(Stream source, T instance, PrefixStyle style)
  299. {
  300. RuntimeTypeModel model = RuntimeTypeModel.Default;
  301. return (T)model.DeserializeWithLengthPrefix(source, instance, model.MapType(typeof(T)), style, 0);
  302. }
  303. /// <summary>
  304. /// Writes a protocol-buffer representation of the given instance to the supplied stream,
  305. /// with a length-prefix. This is useful for socket programming,
  306. /// as DeserializeWithLengthPrefix/MergeWithLengthPrefix can be used to read the single object back
  307. /// from an ongoing stream.
  308. /// </summary>
  309. /// <typeparam name="T">The type being serialized.</typeparam>
  310. /// <param name="instance">The existing instance to be serialized (cannot be null).</param>
  311. /// <param name="style">How to encode the length prefix.</param>
  312. /// <param name="destination">The destination stream to write to.</param>
  313. public static void SerializeWithLengthPrefix<T>(Stream destination, T instance, PrefixStyle style)
  314. {
  315. SerializeWithLengthPrefix<T>(destination, instance, style, 0);
  316. }
  317. /// <summary>
  318. /// Writes a protocol-buffer representation of the given instance to the supplied stream,
  319. /// with a length-prefix. This is useful for socket programming,
  320. /// as DeserializeWithLengthPrefix/MergeWithLengthPrefix can be used to read the single object back
  321. /// from an ongoing stream.
  322. /// </summary>
  323. /// <typeparam name="T">The type being serialized.</typeparam>
  324. /// <param name="instance">The existing instance to be serialized (cannot be null).</param>
  325. /// <param name="style">How to encode the length prefix.</param>
  326. /// <param name="destination">The destination stream to write to.</param>
  327. /// <param name="fieldNumber">The tag used as a prefix to each record (only used with base-128 style prefixes).</param>
  328. public static void SerializeWithLengthPrefix<T>(Stream destination, T instance, PrefixStyle style, int fieldNumber)
  329. {
  330. RuntimeTypeModel model = RuntimeTypeModel.Default;
  331. model.SerializeWithLengthPrefix(destination, instance, model.MapType(typeof(T)), style, fieldNumber);
  332. }
  333. /// <summary>Indicates the number of bytes expected for the next message.</summary>
  334. /// <param name="source">The stream containing the data to investigate for a length.</param>
  335. /// <param name="style">The algorithm used to encode the length.</param>
  336. /// <param name="length">The length of the message, if it could be identified.</param>
  337. /// <returns>True if a length could be obtained, false otherwise.</returns>
  338. public static bool TryReadLengthPrefix(Stream source, PrefixStyle style, out int length)
  339. {
  340. length = ProtoReader.ReadLengthPrefix(source, false, style, out int fieldNumber, out int bytesRead);
  341. return bytesRead > 0;
  342. }
  343. /// <summary>Indicates the number of bytes expected for the next message.</summary>
  344. /// <param name="buffer">The buffer containing the data to investigate for a length.</param>
  345. /// <param name="index">The offset of the first byte to read from the buffer.</param>
  346. /// <param name="count">The number of bytes to read from the buffer.</param>
  347. /// <param name="style">The algorithm used to encode the length.</param>
  348. /// <param name="length">The length of the message, if it could be identified.</param>
  349. /// <returns>True if a length could be obtained, false otherwise.</returns>
  350. public static bool TryReadLengthPrefix(byte[] buffer, int index, int count, PrefixStyle style, out int length)
  351. {
  352. using (Stream source = new MemoryStream(buffer, index, count))
  353. {
  354. return TryReadLengthPrefix(source, style, out length);
  355. }
  356. }
  357. #endif
  358. /// <summary>
  359. /// The field number that is used as a default when serializing/deserializing a list of objects.
  360. /// The data is treated as repeated message with field number 1.
  361. /// </summary>
  362. public const int ListItemTag = 1;
  363. #if !NO_RUNTIME
  364. /// <summary>
  365. /// Provides non-generic access to the default serializer.
  366. /// </summary>
  367. public static class NonGeneric
  368. {
  369. /// <summary>
  370. /// Create a deep clone of the supplied instance; any sub-items are also cloned.
  371. /// </summary>
  372. public static object DeepClone(object instance)
  373. {
  374. return instance == null ? null : RuntimeTypeModel.Default.DeepClone(instance);
  375. }
  376. /// <summary>
  377. /// Writes a protocol-buffer representation of the given instance to the supplied stream.
  378. /// </summary>
  379. /// <param name="instance">The existing instance to be serialized (cannot be null).</param>
  380. /// <param name="dest">The destination stream to write to.</param>
  381. public static void Serialize(Stream dest, object instance)
  382. {
  383. if (instance != null)
  384. {
  385. RuntimeTypeModel.Default.Serialize(dest, instance);
  386. }
  387. }
  388. /// <summary>
  389. /// Creates a new instance from a protocol-buffer stream
  390. /// </summary>
  391. /// <param name="type">The type to be created.</param>
  392. /// <param name="source">The binary stream to apply to the new instance (cannot be null).</param>
  393. /// <returns>A new, initialized instance.</returns>
  394. public static object Deserialize(Type type, Stream source)
  395. {
  396. return RuntimeTypeModel.Default.Deserialize(source, null, type);
  397. }
  398. /// <summary>Applies a protocol-buffer stream to an existing instance.</summary>
  399. /// <param name="instance">The existing instance to be modified (cannot be null).</param>
  400. /// <param name="source">The binary stream to apply to the instance (cannot be null).</param>
  401. /// <returns>The updated instance</returns>
  402. public static object Merge(Stream source, object instance)
  403. {
  404. if (instance == null) throw new ArgumentNullException(nameof(instance));
  405. return RuntimeTypeModel.Default.Deserialize(source, instance, instance.GetType(), null);
  406. }
  407. /// <summary>
  408. /// Writes a protocol-buffer representation of the given instance to the supplied stream,
  409. /// with a length-prefix. This is useful for socket programming,
  410. /// as DeserializeWithLengthPrefix/MergeWithLengthPrefix can be used to read the single object back
  411. /// from an ongoing stream.
  412. /// </summary>
  413. /// <param name="instance">The existing instance to be serialized (cannot be null).</param>
  414. /// <param name="style">How to encode the length prefix.</param>
  415. /// <param name="destination">The destination stream to write to.</param>
  416. /// <param name="fieldNumber">The tag used as a prefix to each record (only used with base-128 style prefixes).</param>
  417. public static void SerializeWithLengthPrefix(Stream destination, object instance, PrefixStyle style, int fieldNumber)
  418. {
  419. if (instance == null) throw new ArgumentNullException(nameof(instance));
  420. RuntimeTypeModel model = RuntimeTypeModel.Default;
  421. model.SerializeWithLengthPrefix(destination, instance, model.MapType(instance.GetType()), style, fieldNumber);
  422. }
  423. /// <summary>
  424. /// Applies a protocol-buffer stream to an existing instance (or null), using length-prefixed
  425. /// data - useful with network IO.
  426. /// </summary>
  427. /// <param name="value">The existing instance to be modified (can be null).</param>
  428. /// <param name="source">The binary stream to apply to the instance (cannot be null).</param>
  429. /// <param name="style">How to encode the length prefix.</param>
  430. /// <param name="resolver">Used to resolve types on a per-field basis.</param>
  431. /// <returns>The updated instance; this may be different to the instance argument if
  432. /// either the original instance was null, or the stream defines a known sub-type of the
  433. /// original instance.</returns>
  434. public static bool TryDeserializeWithLengthPrefix(Stream source, PrefixStyle style, TypeResolver resolver, out object value)
  435. {
  436. value = RuntimeTypeModel.Default.DeserializeWithLengthPrefix(source, null, null, style, 0, resolver);
  437. return value != null;
  438. }
  439. /// <summary>
  440. /// Indicates whether the supplied type is explicitly modelled by the model
  441. /// </summary>
  442. public static bool CanSerialize(Type type) => RuntimeTypeModel.Default.IsDefined(type);
  443. /// <summary>
  444. /// Precompiles the serializer for a given type.
  445. /// </summary>
  446. public static void PrepareSerializer(Type t)
  447. {
  448. #if FEAT_COMPILER
  449. RuntimeTypeModel model = RuntimeTypeModel.Default;
  450. model[model.MapType(t)].CompileInPlace();
  451. #endif
  452. }
  453. }
  454. /// <summary>
  455. /// Global switches that change the behavior of protobuf-net
  456. /// </summary>
  457. public static class GlobalOptions
  458. {
  459. /// <summary>
  460. /// <see cref="RuntimeTypeModel.InferTagFromNameDefault"/>
  461. /// </summary>
  462. [Obsolete("Please use RuntimeTypeModel.Default.InferTagFromNameDefault instead (or on a per-model basis)", false)]
  463. public static bool InferTagFromName
  464. {
  465. get { return RuntimeTypeModel.Default.InferTagFromNameDefault; }
  466. set { RuntimeTypeModel.Default.InferTagFromNameDefault = value; }
  467. }
  468. }
  469. #endif
  470. /// <summary>
  471. /// Maps a field-number to a type
  472. /// </summary>
  473. public delegate Type TypeResolver(int fieldNumber);
  474. /// <summary>
  475. /// Releases any internal buffers that have been reserved for efficiency; this does not affect any serialization
  476. /// operations; simply: it can be used (optionally) to release the buffers for garbage collection (at the expense
  477. /// of having to re-allocate a new buffer for the next operation, rather than re-use prior buffers).
  478. /// </summary>
  479. public static void FlushPool()
  480. {
  481. BufferPool.Flush();
  482. }
  483. }
  484. }