Serializer.cs 27 KB

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