ReadOnlyCollectionSubclassSerializer.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Collections.ObjectModel;
  18. namespace MongoDB.Bson.Serialization.Serializers
  19. {
  20. /// <summary>
  21. /// Represents a serializer for a subclass of ReadOnlyCollection.
  22. /// </summary>
  23. /// <typeparam name="TValue">The type of the value.</typeparam>
  24. /// <typeparam name="TItem">The type of the item.</typeparam>
  25. public class ReadOnlyCollectionSubclassSerializer<TValue, TItem> : EnumerableInterfaceImplementerSerializerBase<TValue, TItem> where TValue : ReadOnlyCollection<TItem>
  26. {
  27. // constructors
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="ReadOnlyCollectionSubclassSerializer{TValue, TItem}"/> class.
  30. /// </summary>
  31. public ReadOnlyCollectionSubclassSerializer()
  32. {
  33. }
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="ReadOnlyCollectionSubclassSerializer{TValue, TItem}"/> class.
  36. /// </summary>
  37. /// <param name="itemSerializer"></param>
  38. public ReadOnlyCollectionSubclassSerializer(IBsonSerializer<TItem> itemSerializer)
  39. : base(itemSerializer)
  40. {
  41. }
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="ReadOnlyCollectionSubclassSerializer{TValue, TItem}"/> class.
  44. /// </summary>
  45. /// <param name="serializerRegistry">The serializer registry.</param>
  46. public ReadOnlyCollectionSubclassSerializer(IBsonSerializerRegistry serializerRegistry)
  47. : base(serializerRegistry)
  48. {
  49. }
  50. // protected methods
  51. /// <summary>
  52. /// Creates the accumulator.
  53. /// </summary>
  54. /// <returns>The accumulator.</returns>
  55. protected override object CreateAccumulator()
  56. {
  57. return new List<TItem>();
  58. }
  59. /// <summary>
  60. /// Finalizes the result.
  61. /// </summary>
  62. /// <param name="accumulator">The accumulator.</param>
  63. /// <returns>The final result.</returns>
  64. protected override TValue FinalizeResult(object accumulator)
  65. {
  66. // the subclass must have a constructor that takes an IList<T> to wrap
  67. return (TValue)Activator.CreateInstance(typeof(TValue), accumulator);
  68. }
  69. }
  70. }