FieldSet.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using Google.ProtocolBuffers.Descriptors;
  6. using Google.ProtocolBuffers.Collections;
  7. namespace Google.ProtocolBuffers {
  8. /// <summary>
  9. /// A class which represents an arbitrary set of fields of some message type.
  10. /// This is used to implement DynamicMessage, and also to represent extensions
  11. /// in GeneratedMessage. This class is internal, since outside users should probably
  12. /// be using DynamicMessage.
  13. ///
  14. /// As in the Java implementation, this class goes against the rest of the framework
  15. /// in terms of mutability. Instead of having a mutable Builder class and an immutable
  16. /// FieldSet class, FieldSet just has a MakeImmutable() method. This is safe so long as
  17. /// all callers are careful not to let a mutable FieldSet escape into the open. This would
  18. /// be impossible to guarantee if this were a public class, of course.
  19. ///
  20. /// All repeated fields are stored as IList[object] even
  21. /// </summary>
  22. internal class FieldSet {
  23. private static readonly FieldSet defaultInstance = new FieldSet(new Dictionary<FieldDescriptor, object>()).MakeImmutable();
  24. private IDictionary<FieldDescriptor, object> fields;
  25. private FieldSet(IDictionary<FieldDescriptor, object> fields) {
  26. this.fields = fields;
  27. }
  28. public static FieldSet CreateInstance() {
  29. // Use SortedList to keep fields in the canonical order
  30. return new FieldSet(new SortedList<FieldDescriptor, object>());
  31. }
  32. /// <summary>
  33. /// Makes this FieldSet immutable, and returns it for convenience. Any
  34. /// mutable repeated fields are made immutable, as well as the map itself.
  35. /// </summary>
  36. internal FieldSet MakeImmutable() {
  37. // First check if we have any repeated values
  38. bool hasRepeats = false;
  39. foreach (object value in fields.Values) {
  40. IList<object> list = value as IList<object>;
  41. if (list != null && !list.IsReadOnly) {
  42. hasRepeats = true;
  43. break;
  44. }
  45. }
  46. if (hasRepeats) {
  47. var tmp = new SortedList<FieldDescriptor, object>();
  48. foreach (KeyValuePair<FieldDescriptor, object> entry in fields) {
  49. IList<object> list = entry.Value as IList<object>;
  50. tmp[entry.Key] = list == null ? entry.Value : Lists.AsReadOnly(list);
  51. }
  52. fields = tmp;
  53. }
  54. fields = Dictionaries.AsReadOnly(fields);
  55. return this;
  56. }
  57. /// <summary>
  58. /// Returns the default, immutable instance with no fields defined.
  59. /// </summary>
  60. internal static FieldSet DefaultInstance {
  61. get { return defaultInstance; }
  62. }
  63. /// <summary>
  64. /// Returns an immutable mapping of fields. Note that although the mapping itself
  65. /// is immutable, the entries may not be (i.e. any repeated values are represented by
  66. /// mutable lists). The behaviour is not specified if the contents are mutated.
  67. /// </summary>
  68. internal IDictionary<FieldDescriptor, object> AllFields {
  69. get { return Dictionaries.AsReadOnly(fields); }
  70. }
  71. /// <summary>
  72. /// See <see cref="IMessage.HasField"/>.
  73. /// </summary>
  74. public bool HasField(FieldDescriptor field) {
  75. if (field.IsRepeated) {
  76. throw new ArgumentException("HasField() can only be called on non-repeated fields.");
  77. }
  78. return fields.ContainsKey(field);
  79. }
  80. // TODO(jonskeet): Should this be in UnknownFieldSet.Builder really? Or CodedInputStream?
  81. internal static void MergeFrom(CodedInputStream input, UnknownFieldSet.Builder unknownFields,
  82. ExtensionRegistry extensionRegistry, IBuilder builder) {
  83. while (true) {
  84. uint tag = input.ReadTag();
  85. if (tag == 0) {
  86. break;
  87. }
  88. if (!MergeFieldFrom(input, unknownFields, extensionRegistry,
  89. builder, tag)) {
  90. // end group tag
  91. break;
  92. }
  93. }
  94. }
  95. // TODO(jonskeet): Should this be in UnknownFieldSet.Builder really? Or CodedInputStream?
  96. /// <summary>
  97. /// Like <see cref="MergeFrom(CodedInputStream, UnknownFieldSet.Builder, ExtensionRegistry, IBuilder)" />
  98. /// but parses a single field.
  99. /// </summary>
  100. /// <param name="input">The input to read the field from</param>
  101. /// <param name="unknownFields">The set of unknown fields to add the newly-read field to, if it's not a known field</param>
  102. /// <param name="extensionRegistry">Registry to use when an extension field is encountered</param>
  103. /// <param name="builder">Builder to merge field into, if it's a known field</param>
  104. /// <param name="tag">The tag, which should already have been read from the input</param>
  105. /// <returns>true unless the tag is an end-group tag</returns>
  106. internal static bool MergeFieldFrom(CodedInputStream input, UnknownFieldSet.Builder unknownFields,
  107. ExtensionRegistry extensionRegistry, IBuilder builder, uint tag) {
  108. MessageDescriptor type = builder.DescriptorForType;
  109. if (type.Options.MessageSetWireFormat && tag == WireFormat.MessageSetTag.ItemStart) {
  110. MergeMessageSetExtensionFromCodedStream(input, unknownFields, extensionRegistry, builder);
  111. return true;
  112. }
  113. WireFormat.WireType wireType = WireFormat.GetTagWireType(tag);
  114. int fieldNumber = WireFormat.GetTagFieldNumber(tag);
  115. FieldDescriptor field;
  116. IMessage defaultFieldInstance = null;
  117. if (type.IsExtensionNumber(fieldNumber)) {
  118. ExtensionInfo extension = extensionRegistry[type, fieldNumber];
  119. if (extension == null) {
  120. field = null;
  121. } else {
  122. field = extension.Descriptor;
  123. defaultFieldInstance = extension.DefaultInstance;
  124. }
  125. } else {
  126. field = type.FindFieldByNumber(fieldNumber);
  127. }
  128. // Unknown field or wrong wire type. Skip.
  129. if (field == null || wireType != WireFormat.FieldTypeToWireFormatMap[field.FieldType]) {
  130. return unknownFields.MergeFieldFrom(tag, input);
  131. }
  132. object value;
  133. switch (field.FieldType) {
  134. case FieldType.Group:
  135. case FieldType.Message: {
  136. IBuilder subBuilder;
  137. if (defaultFieldInstance != null) {
  138. subBuilder = defaultFieldInstance.WeakCreateBuilderForType();
  139. } else {
  140. subBuilder = builder.CreateBuilderForField(field);
  141. }
  142. if (!field.IsRepeated) {
  143. subBuilder.WeakMergeFrom((IMessage) builder[field]);
  144. }
  145. if (field.FieldType == FieldType.Group) {
  146. input.ReadGroup(field.FieldNumber, subBuilder, extensionRegistry);
  147. } else {
  148. input.ReadMessage(subBuilder, extensionRegistry);
  149. }
  150. value = subBuilder.WeakBuild();
  151. break;
  152. }
  153. case FieldType.Enum: {
  154. int rawValue = input.ReadEnum();
  155. value = field.EnumType.FindValueByNumber(rawValue);
  156. // If the number isn't recognized as a valid value for this enum,
  157. // drop it.
  158. if (value == null) {
  159. unknownFields.MergeVarintField(fieldNumber, (ulong) rawValue);
  160. return true;
  161. }
  162. break;
  163. }
  164. default:
  165. value = input.ReadPrimitiveField(field.FieldType);
  166. break;
  167. }
  168. if (field.IsRepeated) {
  169. builder.WeakAddRepeatedField(field, value);
  170. } else {
  171. builder[field] = value;
  172. }
  173. return true;
  174. }
  175. // TODO(jonskeet): Should this be in UnknownFieldSet.Builder really? Or CodedInputStream?
  176. /// <summary>
  177. /// Called by MergeFieldFrom to parse a MessageSet extension.
  178. /// </summary>
  179. private static void MergeMessageSetExtensionFromCodedStream(CodedInputStream input,
  180. UnknownFieldSet.Builder unknownFields,
  181. ExtensionRegistry extensionRegistry,
  182. IBuilder builder) {
  183. MessageDescriptor type = builder.DescriptorForType;
  184. // The wire format for MessageSet is:
  185. // message MessageSet {
  186. // repeated group Item = 1 {
  187. // required int32 typeId = 2;
  188. // required bytes message = 3;
  189. // }
  190. // }
  191. // "typeId" is the extension's field number. The extension can only be
  192. // a message type, where "message" contains the encoded bytes of that
  193. // message.
  194. //
  195. // In practice, we will probably never see a MessageSet item in which
  196. // the message appears before the type ID, or where either field does not
  197. // appear exactly once. However, in theory such cases are valid, so we
  198. // should be prepared to accept them.
  199. int typeId = 0;
  200. ByteString rawBytes = null; // If we encounter "message" before "typeId"
  201. IBuilder subBuilder = null;
  202. FieldDescriptor field = null;
  203. while (true) {
  204. uint tag = input.ReadTag();
  205. if (tag == 0) {
  206. break;
  207. }
  208. if (tag == WireFormat.MessageSetTag.TypeID) {
  209. typeId = input.ReadInt32();
  210. // Zero is not a valid type ID.
  211. if (typeId != 0) {
  212. ExtensionInfo extension = extensionRegistry[type, typeId];
  213. if (extension != null) {
  214. field = extension.Descriptor;
  215. subBuilder = extension.DefaultInstance.WeakCreateBuilderForType();
  216. IMessage originalMessage = (IMessage) builder[field];
  217. if (originalMessage != null) {
  218. subBuilder.WeakMergeFrom(originalMessage);
  219. }
  220. if (rawBytes != null) {
  221. // We already encountered the message. Parse it now.
  222. // TODO(jonskeet): Check this is okay. It's subtly different from the Java, as it doesn't create an input stream from rawBytes.
  223. // In fact, why don't we just call MergeFrom(rawBytes)? And what about the extension registry?
  224. subBuilder.WeakMergeFrom(rawBytes.CreateCodedInput());
  225. rawBytes = null;
  226. }
  227. } else {
  228. // Unknown extension number. If we already saw data, put it
  229. // in rawBytes.
  230. if (rawBytes != null) {
  231. unknownFields.MergeField(typeId,
  232. UnknownField.CreateBuilder()
  233. .AddLengthDelimited(rawBytes)
  234. .Build());
  235. rawBytes = null;
  236. }
  237. }
  238. }
  239. } else if (tag == WireFormat.MessageSetTag.Message) {
  240. if (typeId == 0) {
  241. // We haven't seen a type ID yet, so we have to store the raw bytes for now.
  242. rawBytes = input.ReadBytes();
  243. } else if (subBuilder == null) {
  244. // We don't know how to parse this. Ignore it.
  245. unknownFields.MergeField(typeId,
  246. UnknownField.CreateBuilder()
  247. .AddLengthDelimited(input.ReadBytes())
  248. .Build());
  249. } else {
  250. // We already know the type, so we can parse directly from the input
  251. // with no copying. Hooray!
  252. input.ReadMessage(subBuilder, extensionRegistry);
  253. }
  254. } else {
  255. // Unknown tag. Skip it.
  256. if (!input.SkipField(tag)) {
  257. break; // end of group
  258. }
  259. }
  260. }
  261. input.CheckLastTagWas(WireFormat.MessageSetTag.ItemEnd);
  262. if (subBuilder != null) {
  263. builder[field] = subBuilder.WeakBuild();
  264. }
  265. }
  266. /// <summary>
  267. /// Clears all fields.
  268. /// </summary>
  269. internal void Clear() {
  270. fields.Clear();
  271. }
  272. /// <summary>
  273. /// See <see cref="IMessage.Item(FieldDescriptor)"/>
  274. /// </summary>
  275. /// <remarks>
  276. /// If the field is not set, the behaviour when fetching this property varies by field type:
  277. /// <list>
  278. /// <item>For singular message values, null is returned.</item>
  279. /// <item>For singular non-message values, the default value of the field is returned.</item>
  280. /// <item>For repeated values, an empty immutable list is returned. This will be compatible
  281. /// with IList[object], regardless of the type of the repeated item.</item>
  282. /// </list>
  283. /// This method returns null if the field is a singular message type
  284. /// and is not set; in this case it is up to the caller to fetch the
  285. /// message's default instance. For repeated fields of message types,
  286. /// an empty collection is returned. For repeated fields of non-message
  287. /// types, null is returned.
  288. /// <para />
  289. /// When setting this property, any list values are copied, and each element is checked
  290. /// to ensure it is of an appropriate type.
  291. /// </remarks>
  292. ///
  293. internal object this[FieldDescriptor field] {
  294. get {
  295. object result;
  296. if (fields.TryGetValue(field, out result)) {
  297. return result;
  298. }
  299. if (field.MappedType == MappedType.Message) {
  300. if (field.IsRepeated) {
  301. return new List<object>();
  302. } else {
  303. return null;
  304. }
  305. }
  306. return field.DefaultValue;
  307. }
  308. set {
  309. if (field.IsRepeated) {
  310. List<object> list = value as List<object>;
  311. if (list == null) {
  312. throw new ArgumentException("Wrong object type used with protocol message reflection.");
  313. }
  314. // Wrap the contents in a new list so that the caller cannot change
  315. // the list's contents after setting it.
  316. List<object> newList = new List<object>(list);
  317. foreach (object element in newList) {
  318. VerifyType(field, element);
  319. }
  320. value = newList;
  321. }
  322. else {
  323. VerifyType(field, value);
  324. }
  325. fields[field] = value;
  326. }
  327. }
  328. /// <summary>
  329. /// See <see cref="IMessage.Item(FieldDescriptor,int)" />
  330. /// </summary>
  331. internal object this[FieldDescriptor field, int index] {
  332. get {
  333. if (!field.IsRepeated) {
  334. throw new ArgumentException("Indexer specifying field and index can only be called on repeated fields.");
  335. }
  336. return ((IList<object>) this[field])[index];
  337. }
  338. set {
  339. if (!field.IsRepeated) {
  340. throw new ArgumentException("Indexer specifying field and index can only be called on repeated fields.");
  341. }
  342. VerifyType(field, value);
  343. object list;
  344. if (!fields.TryGetValue(field, out list)) {
  345. throw new ArgumentOutOfRangeException();
  346. }
  347. ((IList<object>) list)[index] = value;
  348. }
  349. }
  350. /// <summary>
  351. /// See <see cref="IBuilder{TMessage, TBuilder}.AddRepeatedField" />
  352. /// </summary>
  353. internal void AddRepeatedField(FieldDescriptor field, object value) {
  354. if (!field.IsRepeated) {
  355. throw new ArgumentException("AddRepeatedField can only be called on repeated fields.");
  356. }
  357. VerifyType(field, value);
  358. object list;
  359. if (!fields.TryGetValue(field, out list)) {
  360. list = new List<object>();
  361. fields[field] = list;
  362. }
  363. ((IList<object>) list).Add(value);
  364. }
  365. /// <summary>
  366. /// Returns an enumerator for the field map. Used to write the fields out.
  367. /// </summary>
  368. internal IEnumerator<KeyValuePair<FieldDescriptor, object>> GetEnumerator() {
  369. return fields.GetEnumerator();
  370. }
  371. /// <summary>
  372. /// See <see cref="IMessage.IsInitialized" />
  373. /// </summary>
  374. /// <remarks>
  375. /// Since FieldSet itself does not have any way of knowing about
  376. /// required fields that aren't actually present in the set, it is up
  377. /// to the caller to check for genuinely required fields. This property
  378. /// merely checks that any messages present are themselves initialized.
  379. /// </remarks>
  380. internal bool IsInitialized {
  381. get {
  382. foreach (KeyValuePair<FieldDescriptor, object> entry in fields) {
  383. FieldDescriptor field = entry.Key;
  384. if (field.MappedType == MappedType.Message) {
  385. if (field.IsRepeated) {
  386. foreach(IMessage message in (IEnumerable) entry.Value) {
  387. if (!message.IsInitialized) {
  388. return false;
  389. }
  390. }
  391. } else {
  392. if (!((IMessage) entry.Value).IsInitialized) {
  393. return false;
  394. }
  395. }
  396. }
  397. }
  398. return true;
  399. }
  400. }
  401. /// <summary>
  402. /// Verifies whether all the required fields in the specified message
  403. /// descriptor are present in this field set, as well as whether
  404. /// all the embedded messages are themselves initialized.
  405. /// </summary>
  406. internal bool IsInitializedWithRespectTo(MessageDescriptor type) {
  407. foreach (FieldDescriptor field in type.Fields) {
  408. if (field.IsRequired && !HasField(field)) {
  409. return false;
  410. }
  411. }
  412. return IsInitialized;
  413. }
  414. /// <summary>
  415. /// See <see cref="IBuilder{TMessage, TBuilder}.ClearField" />
  416. /// </summary>
  417. public void ClearField(FieldDescriptor field) {
  418. fields.Remove(field);
  419. }
  420. /// <summary>
  421. /// See <see cref="IMessage.GetRepeatedFieldCount" />
  422. /// </summary>
  423. public int GetRepeatedFieldCount(FieldDescriptor field) {
  424. if (!field.IsRepeated) {
  425. throw new ArgumentException("GetRepeatedFieldCount() can only be called on repeated fields.");
  426. }
  427. return ((IList<object>) this[field]).Count;
  428. }
  429. /// <summary>
  430. /// Implementation of both <c>MergeFrom</c> methods.
  431. /// </summary>
  432. /// <param name="otherFields"></param>
  433. private void MergeFields(IEnumerable<KeyValuePair<FieldDescriptor, object>> otherFields) {
  434. // Note: We don't attempt to verify that other's fields have valid
  435. // types. Doing so would be a losing battle. We'd have to verify
  436. // all sub-messages as well, and we'd have to make copies of all of
  437. // them to insure that they don't change after verification (since
  438. // the IMessage interface itself cannot enforce immutability of
  439. // implementations).
  440. // TODO(jonskeet): Provide a function somewhere called MakeDeepCopy()
  441. // which allows people to make secure deep copies of messages.
  442. foreach (KeyValuePair<FieldDescriptor, object> entry in otherFields) {
  443. FieldDescriptor field = entry.Key;
  444. object existingValue;
  445. fields.TryGetValue(field, out existingValue);
  446. if (field.IsRepeated) {
  447. if (existingValue == null) {
  448. existingValue = new List<object>();
  449. fields[field] = existingValue;
  450. }
  451. IList<object> list = (IList<object>) existingValue;
  452. foreach (object otherValue in (IEnumerable) entry.Value) {
  453. list.Add(otherValue);
  454. }
  455. } else if (field.MappedType == MappedType.Message && existingValue != null) {
  456. IMessage existingMessage = (IMessage)existingValue;
  457. IMessage merged = existingMessage.WeakCreateBuilderForType()
  458. .WeakMergeFrom(existingMessage)
  459. .WeakMergeFrom((IMessage) entry.Value)
  460. .WeakBuild();
  461. this[field] = merged;
  462. } else {
  463. this[field] = entry.Value;
  464. }
  465. }
  466. }
  467. /// <summary>
  468. /// See <see cref="IBuilder{TMessage, TBuilder}.MergeFrom(IMessage)" />
  469. /// </summary>
  470. public void MergeFrom(IMessage other) {
  471. MergeFields(other.AllFields);
  472. }
  473. /// <summary>
  474. /// Like <see cref="MergeFrom(IMessage)"/>, but merges from another <c>FieldSet</c>.
  475. /// </summary>
  476. public void MergeFrom(FieldSet other) {
  477. MergeFields(other.fields);
  478. }
  479. /// <summary>
  480. /// See <see cref="IMessage.WriteTo(CodedOutputStream)" />.
  481. /// </summary>
  482. public void WriteTo(CodedOutputStream output) {
  483. foreach (KeyValuePair<FieldDescriptor, object> entry in fields) {
  484. WriteField(entry.Key, entry.Value, output);
  485. }
  486. }
  487. /// <summary>
  488. /// Writes a single field to a CodedOutputStream.
  489. /// </summary>
  490. public void WriteField(FieldDescriptor field, Object value, CodedOutputStream output) {
  491. if (field.IsExtension && field.ContainingType.Options.MessageSetWireFormat) {
  492. output.WriteMessageSetExtension(field.FieldNumber, (IMessage) value);
  493. } else {
  494. if (field.IsRepeated) {
  495. foreach (object element in (IEnumerable) value) {
  496. output.WriteField(field.FieldType, field.FieldNumber, element);
  497. }
  498. } else {
  499. output.WriteField(field.FieldType, field.FieldNumber, value);
  500. }
  501. }
  502. }
  503. /// <summary>
  504. /// See <see cref="IMessage.SerializedSize" />. It's up to the caller to
  505. /// cache the resulting size if desired.
  506. /// </summary>
  507. public int SerializedSize {
  508. get {
  509. int size = 0;
  510. foreach (KeyValuePair<FieldDescriptor, object> entry in fields) {
  511. FieldDescriptor field = entry.Key;
  512. object value = entry.Value;
  513. if (field.IsExtension && field.ContainingType.Options.MessageSetWireFormat) {
  514. size += CodedOutputStream.ComputeMessageSetExtensionSize(field.FieldNumber, (IMessage) value);
  515. } else {
  516. if (field.IsRepeated) {
  517. foreach (object element in (IEnumerable) value) {
  518. size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, element);
  519. }
  520. } else {
  521. size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, value);
  522. }
  523. }
  524. }
  525. return size;
  526. }
  527. }
  528. /// <summary>
  529. /// Verifies that the given object is of the correct type to be a valid
  530. /// value for the given field.
  531. /// </summary>
  532. /// <remarks>
  533. /// For repeated fields, this checks if the object is of the right
  534. /// element type, not whether it's a list.
  535. /// </remarks>
  536. /// <exception cref="ArgumentException">The value is not of the right type.</exception>
  537. private static void VerifyType(FieldDescriptor field, object value) {
  538. bool isValid = false;
  539. switch (field.MappedType) {
  540. case MappedType.Int32: isValid = value is int; break;
  541. case MappedType.Int64: isValid = value is long; break;
  542. case MappedType.UInt32: isValid = value is uint; break;
  543. case MappedType.UInt64: isValid = value is ulong; break;
  544. case MappedType.Single: isValid = value is float; break;
  545. case MappedType.Double: isValid = value is double; break;
  546. case MappedType.Boolean: isValid = value is bool; break;
  547. case MappedType.String: isValid = value is string; break;
  548. case MappedType.ByteString: isValid = value is ByteString; break;
  549. case MappedType.Enum:
  550. EnumValueDescriptor enumValue = value as EnumValueDescriptor;
  551. isValid = enumValue != null && enumValue.EnumDescriptor == field.EnumType;
  552. break;
  553. case MappedType.Message:
  554. IMessage messageValue = value as IMessage;
  555. isValid = messageValue != null && messageValue.DescriptorForType == field.MessageType;
  556. break;
  557. }
  558. if (!isValid) {
  559. // When chaining calls to SetField(), it can be hard to tell from
  560. // the stack trace which exact call failed, since the whole chain is
  561. // considered one line of code. So, let's make sure to include the
  562. // field name and other useful info in the exception.
  563. throw new ArgumentException("Wrong object type used with protocol message reflection. "
  564. + "Message type \"" + field.ContainingType.FullName
  565. + "\", field \"" + (field.IsExtension ? field.FullName : field.Name)
  566. + "\", value was type \"" + value.GetType().Name + "\".");
  567. }
  568. }
  569. }
  570. }