BclHelpers.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. using System;
  2. using System.Reflection;
  3. namespace ProtoBuf
  4. {
  5. internal enum TimeSpanScale
  6. {
  7. Days = 0,
  8. Hours = 1,
  9. Minutes = 2,
  10. Seconds = 3,
  11. Milliseconds = 4,
  12. Ticks = 5,
  13. MinMax = 15
  14. }
  15. /// <summary>
  16. /// Provides support for common .NET types that do not have a direct representation
  17. /// in protobuf, using the definitions from bcl.proto
  18. /// </summary>
  19. public
  20. #if FX11
  21. sealed
  22. #else
  23. static
  24. #endif
  25. class BclHelpers
  26. {
  27. /// <summary>
  28. /// Creates a new instance of the specified type, bypassing the constructor.
  29. /// </summary>
  30. /// <param name="type">The type to create</param>
  31. /// <returns>The new instance</returns>
  32. /// <exception cref="NotSupportedException">If the platform does not support constructor-skipping</exception>
  33. public static object GetUninitializedObject(Type type)
  34. {
  35. #if COREFX
  36. object obj = TryGetUninitializedObjectWithFormatterServices(type);
  37. if (obj != null) return obj;
  38. #endif
  39. #if PLAT_BINARYFORMATTER && !(WINRT || PHONE8 || COREFX)
  40. return System.Runtime.Serialization.FormatterServices.GetUninitializedObject(type);
  41. #else
  42. throw new NotSupportedException("Constructor-skipping is not supported on this platform");
  43. #endif
  44. }
  45. #if COREFX // this is inspired by DCS: https://github.com/dotnet/corefx/blob/c02d33b18398199f6acc17d375dab154e9a1df66/src/System.Private.DataContractSerialization/src/System/Runtime/Serialization/XmlFormatReaderGenerator.cs#L854-L894
  46. static Func<Type, object> getUninitializedObject;
  47. static internal object TryGetUninitializedObjectWithFormatterServices(Type type)
  48. {
  49. if (getUninitializedObject == null)
  50. {
  51. try {
  52. var formatterServiceType = typeof(string).GetTypeInfo().Assembly.GetType("System.Runtime.Serialization.FormatterServices");
  53. MethodInfo method = formatterServiceType?.GetMethod("GetUninitializedObject", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
  54. if (method != null)
  55. {
  56. getUninitializedObject = (Func<Type, object>)method.CreateDelegate(typeof(Func<Type, object>));
  57. }
  58. }
  59. catch { /* best efforts only */ }
  60. if(getUninitializedObject == null) getUninitializedObject = x => null;
  61. }
  62. return getUninitializedObject(type);
  63. }
  64. #endif
  65. #if FX11
  66. private BclHelpers() { } // not a static class for C# 1.2 reasons
  67. #endif
  68. const int FieldTimeSpanValue = 0x01, FieldTimeSpanScale = 0x02, FieldTimeSpanKind = 0x03;
  69. internal static readonly DateTime[] EpochOrigin = {
  70. new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified),
  71. new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc),
  72. new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Local)
  73. };
  74. /// <summary>
  75. /// Writes a TimeSpan to a protobuf stream
  76. /// </summary>
  77. public static void WriteTimeSpan(TimeSpan timeSpan, ProtoWriter dest)
  78. {
  79. WriteTimeSpanImpl(timeSpan, dest, DateTimeKind.Unspecified);
  80. }
  81. private static void WriteTimeSpanImpl(TimeSpan timeSpan, ProtoWriter dest, DateTimeKind kind)
  82. {
  83. if (dest == null) throw new ArgumentNullException("dest");
  84. long value;
  85. switch(dest.WireType)
  86. {
  87. case WireType.String:
  88. case WireType.StartGroup:
  89. TimeSpanScale scale;
  90. value = timeSpan.Ticks;
  91. if (timeSpan == TimeSpan.MaxValue)
  92. {
  93. value = 1;
  94. scale = TimeSpanScale.MinMax;
  95. }
  96. else if (timeSpan == TimeSpan.MinValue)
  97. {
  98. value = -1;
  99. scale = TimeSpanScale.MinMax;
  100. }
  101. else if (value % TimeSpan.TicksPerDay == 0)
  102. {
  103. scale = TimeSpanScale.Days;
  104. value /= TimeSpan.TicksPerDay;
  105. }
  106. else if (value % TimeSpan.TicksPerHour == 0)
  107. {
  108. scale = TimeSpanScale.Hours;
  109. value /= TimeSpan.TicksPerHour;
  110. }
  111. else if (value % TimeSpan.TicksPerMinute == 0)
  112. {
  113. scale = TimeSpanScale.Minutes;
  114. value /= TimeSpan.TicksPerMinute;
  115. }
  116. else if (value % TimeSpan.TicksPerSecond == 0)
  117. {
  118. scale = TimeSpanScale.Seconds;
  119. value /= TimeSpan.TicksPerSecond;
  120. }
  121. else if (value % TimeSpan.TicksPerMillisecond == 0)
  122. {
  123. scale = TimeSpanScale.Milliseconds;
  124. value /= TimeSpan.TicksPerMillisecond;
  125. }
  126. else
  127. {
  128. scale = TimeSpanScale.Ticks;
  129. }
  130. SubItemToken token = ProtoWriter.StartSubItem(null, dest);
  131. if(value != 0) {
  132. ProtoWriter.WriteFieldHeader(FieldTimeSpanValue, WireType.SignedVariant, dest);
  133. ProtoWriter.WriteInt64(value, dest);
  134. }
  135. if(scale != TimeSpanScale.Days) {
  136. ProtoWriter.WriteFieldHeader(FieldTimeSpanScale, WireType.Variant, dest);
  137. ProtoWriter.WriteInt32((int)scale, dest);
  138. }
  139. if(kind != DateTimeKind.Unspecified)
  140. {
  141. ProtoWriter.WriteFieldHeader(FieldTimeSpanKind, WireType.Variant, dest);
  142. ProtoWriter.WriteInt32((int)kind, dest);
  143. }
  144. ProtoWriter.EndSubItem(token, dest);
  145. break;
  146. case WireType.Fixed64:
  147. ProtoWriter.WriteInt64(timeSpan.Ticks, dest);
  148. break;
  149. default:
  150. throw new ProtoException("Unexpected wire-type: " + dest.WireType.ToString());
  151. }
  152. }
  153. /// <summary>
  154. /// Parses a TimeSpan from a protobuf stream
  155. /// </summary>
  156. public static TimeSpan ReadTimeSpan(ProtoReader source)
  157. {
  158. DateTimeKind kind;
  159. long ticks = ReadTimeSpanTicks(source, out kind);
  160. if (ticks == long.MinValue) return TimeSpan.MinValue;
  161. if (ticks == long.MaxValue) return TimeSpan.MaxValue;
  162. return TimeSpan.FromTicks(ticks);
  163. }
  164. /// <summary>
  165. /// Parses a DateTime from a protobuf stream
  166. /// </summary>
  167. public static DateTime ReadDateTime(ProtoReader source)
  168. {
  169. DateTimeKind kind;
  170. long ticks = ReadTimeSpanTicks(source, out kind);
  171. if (ticks == long.MinValue) return DateTime.MinValue;
  172. if (ticks == long.MaxValue) return DateTime.MaxValue;
  173. return EpochOrigin[(int)kind].AddTicks(ticks);
  174. }
  175. /// <summary>
  176. /// Writes a DateTime to a protobuf stream, excluding the <c>Kind</c>
  177. /// </summary>
  178. public static void WriteDateTime(DateTime value, ProtoWriter dest)
  179. {
  180. WriteDateTimeImpl(value, dest, false);
  181. }
  182. /// <summary>
  183. /// Writes a DateTime to a protobuf stream, including the <c>Kind</c>
  184. /// </summary>
  185. public static void WriteDateTimeWithKind(DateTime value, ProtoWriter dest)
  186. {
  187. WriteDateTimeImpl(value, dest, true);
  188. }
  189. private static void WriteDateTimeImpl(DateTime value, ProtoWriter dest, bool includeKind)
  190. {
  191. if (dest == null) throw new ArgumentNullException("dest");
  192. TimeSpan delta;
  193. switch (dest.WireType)
  194. {
  195. case WireType.StartGroup:
  196. case WireType.String:
  197. if (value == DateTime.MaxValue)
  198. {
  199. delta = TimeSpan.MaxValue;
  200. includeKind = false;
  201. }
  202. else if (value == DateTime.MinValue)
  203. {
  204. delta = TimeSpan.MinValue;
  205. includeKind = false;
  206. }
  207. else
  208. {
  209. delta = value - EpochOrigin[0];
  210. }
  211. break;
  212. default:
  213. delta = value - EpochOrigin[0];
  214. break;
  215. }
  216. WriteTimeSpanImpl(delta, dest, includeKind ? value.Kind : DateTimeKind.Unspecified);
  217. }
  218. private static long ReadTimeSpanTicks(ProtoReader source, out DateTimeKind kind) {
  219. kind = DateTimeKind.Unspecified;
  220. switch (source.WireType)
  221. {
  222. case WireType.String:
  223. case WireType.StartGroup:
  224. SubItemToken token = ProtoReader.StartSubItem(source);
  225. int fieldNumber;
  226. TimeSpanScale scale = TimeSpanScale.Days;
  227. long value = 0;
  228. while ((fieldNumber = source.ReadFieldHeader()) > 0)
  229. {
  230. switch (fieldNumber)
  231. {
  232. case FieldTimeSpanScale:
  233. scale = (TimeSpanScale)source.ReadInt32();
  234. break;
  235. case FieldTimeSpanValue:
  236. source.Assert(WireType.SignedVariant);
  237. value = source.ReadInt64();
  238. break;
  239. case FieldTimeSpanKind:
  240. kind = (DateTimeKind)source.ReadInt32();
  241. switch(kind)
  242. {
  243. case DateTimeKind.Unspecified:
  244. case DateTimeKind.Utc:
  245. case DateTimeKind.Local:
  246. break; // fine
  247. default:
  248. throw new ProtoException("Invalid date/time kind: " + kind.ToString());
  249. }
  250. break;
  251. default:
  252. source.SkipField();
  253. break;
  254. }
  255. }
  256. ProtoReader.EndSubItem(token, source);
  257. switch (scale)
  258. {
  259. case TimeSpanScale.Days:
  260. return value * TimeSpan.TicksPerDay;
  261. case TimeSpanScale.Hours:
  262. return value * TimeSpan.TicksPerHour;
  263. case TimeSpanScale.Minutes:
  264. return value * TimeSpan.TicksPerMinute;
  265. case TimeSpanScale.Seconds:
  266. return value * TimeSpan.TicksPerSecond;
  267. case TimeSpanScale.Milliseconds:
  268. return value * TimeSpan.TicksPerMillisecond;
  269. case TimeSpanScale.Ticks:
  270. return value;
  271. case TimeSpanScale.MinMax:
  272. switch (value)
  273. {
  274. case 1: return long.MaxValue;
  275. case -1: return long.MinValue;
  276. default: throw new ProtoException("Unknown min/max value: " + value.ToString());
  277. }
  278. default:
  279. throw new ProtoException("Unknown timescale: " + scale.ToString());
  280. }
  281. case WireType.Fixed64:
  282. return source.ReadInt64();
  283. default:
  284. throw new ProtoException("Unexpected wire-type: " + source.WireType.ToString());
  285. }
  286. }
  287. const int FieldDecimalLow = 0x01, FieldDecimalHigh = 0x02, FieldDecimalSignScale = 0x03;
  288. /// <summary>
  289. /// Parses a decimal from a protobuf stream
  290. /// </summary>
  291. public static decimal ReadDecimal(ProtoReader reader)
  292. {
  293. ulong low = 0;
  294. uint high = 0;
  295. uint signScale = 0;
  296. int fieldNumber;
  297. SubItemToken token = ProtoReader.StartSubItem(reader);
  298. while ((fieldNumber = reader.ReadFieldHeader()) > 0)
  299. {
  300. switch (fieldNumber)
  301. {
  302. case FieldDecimalLow: low = reader.ReadUInt64(); break;
  303. case FieldDecimalHigh: high = reader.ReadUInt32(); break;
  304. case FieldDecimalSignScale: signScale = reader.ReadUInt32(); break;
  305. default: reader.SkipField(); break;
  306. }
  307. }
  308. ProtoReader.EndSubItem(token, reader);
  309. if (low == 0 && high == 0) return decimal.Zero;
  310. int lo = (int)(low & 0xFFFFFFFFL),
  311. mid = (int)((low >> 32) & 0xFFFFFFFFL),
  312. hi = (int)high;
  313. bool isNeg = (signScale & 0x0001) == 0x0001;
  314. byte scale = (byte)((signScale & 0x01FE) >> 1);
  315. return new decimal(lo, mid, hi, isNeg, scale);
  316. }
  317. /// <summary>
  318. /// Writes a decimal to a protobuf stream
  319. /// </summary>
  320. public static void WriteDecimal(decimal value, ProtoWriter writer)
  321. {
  322. int[] bits = decimal.GetBits(value);
  323. ulong a = ((ulong)bits[1]) << 32, b = ((ulong)bits[0]) & 0xFFFFFFFFL;
  324. ulong low = a | b;
  325. uint high = (uint)bits[2];
  326. uint signScale = (uint)(((bits[3] >> 15) & 0x01FE) | ((bits[3] >> 31) & 0x0001));
  327. SubItemToken token = ProtoWriter.StartSubItem(null, writer);
  328. if (low != 0) {
  329. ProtoWriter.WriteFieldHeader(FieldDecimalLow, WireType.Variant, writer);
  330. ProtoWriter.WriteUInt64(low, writer);
  331. }
  332. if (high != 0)
  333. {
  334. ProtoWriter.WriteFieldHeader(FieldDecimalHigh, WireType.Variant, writer);
  335. ProtoWriter.WriteUInt32(high, writer);
  336. }
  337. if (signScale != 0)
  338. {
  339. ProtoWriter.WriteFieldHeader(FieldDecimalSignScale, WireType.Variant, writer);
  340. ProtoWriter.WriteUInt32(signScale, writer);
  341. }
  342. ProtoWriter.EndSubItem(token, writer);
  343. }
  344. const int FieldGuidLow = 1, FieldGuidHigh = 2;
  345. /// <summary>
  346. /// Writes a Guid to a protobuf stream
  347. /// </summary>
  348. public static void WriteGuid(Guid value, ProtoWriter dest)
  349. {
  350. byte[] blob = value.ToByteArray();
  351. SubItemToken token = ProtoWriter.StartSubItem(null, dest);
  352. if (value != Guid.Empty)
  353. {
  354. ProtoWriter.WriteFieldHeader(FieldGuidLow, WireType.Fixed64, dest);
  355. ProtoWriter.WriteBytes(blob, 0, 8, dest);
  356. ProtoWriter.WriteFieldHeader(FieldGuidHigh, WireType.Fixed64, dest);
  357. ProtoWriter.WriteBytes(blob, 8, 8, dest);
  358. }
  359. ProtoWriter.EndSubItem(token, dest);
  360. }
  361. /// <summary>
  362. /// Parses a Guid from a protobuf stream
  363. /// </summary>
  364. public static Guid ReadGuid(ProtoReader source)
  365. {
  366. ulong low = 0, high = 0;
  367. int fieldNumber;
  368. SubItemToken token = ProtoReader.StartSubItem(source);
  369. while ((fieldNumber = source.ReadFieldHeader()) > 0)
  370. {
  371. switch (fieldNumber)
  372. {
  373. case FieldGuidLow: low = source.ReadUInt64(); break;
  374. case FieldGuidHigh: high = source.ReadUInt64(); break;
  375. default: source.SkipField(); break;
  376. }
  377. }
  378. ProtoReader.EndSubItem(token, source);
  379. if(low == 0 && high == 0) return Guid.Empty;
  380. uint a = (uint)(low >> 32), b = (uint)low, c = (uint)(high >> 32), d= (uint)high;
  381. return new Guid((int)b, (short)a, (short)(a >> 16),
  382. (byte)d, (byte)(d >> 8), (byte)(d >> 16), (byte)(d >> 24),
  383. (byte)c, (byte)(c >> 8), (byte)(c >> 16), (byte)(c >> 24));
  384. }
  385. private const int
  386. FieldExistingObjectKey = 1,
  387. FieldNewObjectKey = 2,
  388. FieldExistingTypeKey = 3,
  389. FieldNewTypeKey = 4,
  390. FieldTypeName = 8,
  391. FieldObject = 10;
  392. /// <summary>
  393. /// Optional behaviours that introduce .NET-specific functionality
  394. /// </summary>
  395. [Flags]
  396. public enum NetObjectOptions : byte
  397. {
  398. /// <summary>
  399. /// No special behaviour
  400. /// </summary>
  401. None = 0,
  402. /// <summary>
  403. /// Enables full object-tracking/full-graph support.
  404. /// </summary>
  405. AsReference = 1,
  406. /// <summary>
  407. /// Embeds the type information into the stream, allowing usage with types not known in advance.
  408. /// </summary>
  409. DynamicType = 2,
  410. /// <summary>
  411. /// If false, the constructor for the type is bypassed during deserialization, meaning any field initializers
  412. /// or other initialization code is skipped.
  413. /// </summary>
  414. UseConstructor = 4,
  415. /// <summary>
  416. /// Should the object index be reserved, rather than creating an object promptly
  417. /// </summary>
  418. LateSet = 8
  419. }
  420. /// <summary>
  421. /// Reads an *implementation specific* bundled .NET object, including (as options) type-metadata, identity/re-use, etc.
  422. /// </summary>
  423. public static object ReadNetObject(object value, ProtoReader source, int key, Type type, NetObjectOptions options)
  424. {
  425. #if FEAT_IKVM
  426. throw new NotSupportedException();
  427. #else
  428. SubItemToken token = ProtoReader.StartSubItem(source);
  429. int fieldNumber;
  430. int newObjectKey = -1, newTypeKey = -1, tmp;
  431. while ((fieldNumber = source.ReadFieldHeader()) > 0)
  432. {
  433. switch (fieldNumber)
  434. {
  435. case FieldExistingObjectKey:
  436. tmp = source.ReadInt32();
  437. value = source.NetCache.GetKeyedObject(tmp);
  438. break;
  439. case FieldNewObjectKey:
  440. newObjectKey = source.ReadInt32();
  441. break;
  442. case FieldExistingTypeKey:
  443. tmp = source.ReadInt32();
  444. type = (Type)source.NetCache.GetKeyedObject(tmp);
  445. key = source.GetTypeKey(ref type);
  446. break;
  447. case FieldNewTypeKey:
  448. newTypeKey = source.ReadInt32();
  449. break;
  450. case FieldTypeName:
  451. string typeName = source.ReadString();
  452. type = source.DeserializeType(typeName);
  453. if(type == null)
  454. {
  455. throw new ProtoException("Unable to resolve type: " + typeName + " (you can use the TypeModel.DynamicTypeFormatting event to provide a custom mapping)");
  456. }
  457. if (type == typeof(string))
  458. {
  459. key = -1;
  460. }
  461. else
  462. {
  463. key = source.GetTypeKey(ref type);
  464. if (key < 0)
  465. throw new InvalidOperationException("Dynamic type is not a contract-type: " + type.Name);
  466. }
  467. break;
  468. case FieldObject:
  469. bool isString = type == typeof(string);
  470. bool wasNull = value == null;
  471. bool lateSet = wasNull && (isString || ((options & NetObjectOptions.LateSet) != 0));
  472. if (newObjectKey >= 0 && !lateSet)
  473. {
  474. if (value == null)
  475. {
  476. source.TrapNextObject(newObjectKey);
  477. }
  478. else
  479. {
  480. source.NetCache.SetKeyedObject(newObjectKey, value);
  481. }
  482. if (newTypeKey >= 0) source.NetCache.SetKeyedObject(newTypeKey, type);
  483. }
  484. object oldValue = value;
  485. if (isString)
  486. {
  487. value = source.ReadString();
  488. }
  489. else
  490. {
  491. value = ProtoReader.ReadTypedObject(oldValue, key, source, type);
  492. }
  493. if (newObjectKey >= 0)
  494. {
  495. if(wasNull && !lateSet)
  496. { // this both ensures (via exception) that it *was* set, and makes sure we don't shout
  497. // about changed references
  498. oldValue = source.NetCache.GetKeyedObject(newObjectKey);
  499. }
  500. if (lateSet)
  501. {
  502. source.NetCache.SetKeyedObject(newObjectKey, value);
  503. if (newTypeKey >= 0) source.NetCache.SetKeyedObject(newTypeKey, type);
  504. }
  505. }
  506. if (newObjectKey >= 0 && !lateSet && !ReferenceEquals(oldValue, value))
  507. {
  508. throw new ProtoException("A reference-tracked object changed reference during deserialization");
  509. }
  510. if (newObjectKey < 0 && newTypeKey >= 0)
  511. { // have a new type, but not a new object
  512. source.NetCache.SetKeyedObject(newTypeKey, type);
  513. }
  514. break;
  515. default:
  516. source.SkipField();
  517. break;
  518. }
  519. }
  520. if(newObjectKey >= 0 && (options & NetObjectOptions.AsReference) == 0)
  521. {
  522. throw new ProtoException("Object key in input stream, but reference-tracking was not expected");
  523. }
  524. ProtoReader.EndSubItem(token, source);
  525. return value;
  526. #endif
  527. }
  528. /// <summary>
  529. /// Writes an *implementation specific* bundled .NET object, including (as options) type-metadata, identity/re-use, etc.
  530. /// </summary>
  531. public static void WriteNetObject(object value, ProtoWriter dest, int key, NetObjectOptions options)
  532. {
  533. #if FEAT_IKVM
  534. throw new NotSupportedException();
  535. #else
  536. if (dest == null) throw new ArgumentNullException("dest");
  537. bool dynamicType = (options & NetObjectOptions.DynamicType) != 0,
  538. asReference = (options & NetObjectOptions.AsReference) != 0;
  539. WireType wireType = dest.WireType;
  540. SubItemToken token = ProtoWriter.StartSubItem(null, dest);
  541. bool writeObject = true;
  542. if (asReference)
  543. {
  544. bool existing;
  545. int objectKey = dest.NetCache.AddObjectKey(value, out existing);
  546. ProtoWriter.WriteFieldHeader(existing ? FieldExistingObjectKey : FieldNewObjectKey, WireType.Variant, dest);
  547. ProtoWriter.WriteInt32(objectKey, dest);
  548. if (existing)
  549. {
  550. writeObject = false;
  551. }
  552. }
  553. if (writeObject)
  554. {
  555. if (dynamicType)
  556. {
  557. bool existing;
  558. Type type = value.GetType();
  559. if (!(value is string))
  560. {
  561. key = dest.GetTypeKey(ref type);
  562. if (key < 0) throw new InvalidOperationException("Dynamic type is not a contract-type: " + type.Name);
  563. }
  564. int typeKey = dest.NetCache.AddObjectKey(type, out existing);
  565. ProtoWriter.WriteFieldHeader(existing ? FieldExistingTypeKey : FieldNewTypeKey, WireType.Variant, dest);
  566. ProtoWriter.WriteInt32(typeKey, dest);
  567. if (!existing)
  568. {
  569. ProtoWriter.WriteFieldHeader(FieldTypeName, WireType.String, dest);
  570. ProtoWriter.WriteString(dest.SerializeType(type), dest);
  571. }
  572. }
  573. ProtoWriter.WriteFieldHeader(FieldObject, wireType, dest);
  574. if (value is string)
  575. {
  576. ProtoWriter.WriteString((string)value, dest);
  577. }
  578. else {
  579. ProtoWriter.WriteObject(value, key, dest);
  580. }
  581. }
  582. ProtoWriter.EndSubItem(token, dest);
  583. #endif
  584. }
  585. }
  586. }