BsonTimeSpanOptionsAttribute.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 MongoDB.Bson.Serialization.Options;
  17. using MongoDB.Bson.Serialization.Serializers;
  18. namespace MongoDB.Bson.Serialization.Attributes
  19. {
  20. /// <summary>
  21. /// Specifies the external representation and related options for this field or property.
  22. /// </summary>
  23. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
  24. public class BsonTimeSpanOptionsAttribute : BsonSerializationOptionsAttribute
  25. {
  26. // private fields
  27. private BsonType _representation;
  28. private TimeSpanUnits _units;
  29. // constructors
  30. /// <summary>
  31. /// Initializes a new instance of the BsonTimeSpanOptionsAttribute class.
  32. /// </summary>
  33. /// <param name="representation">The external representation.</param>
  34. public BsonTimeSpanOptionsAttribute(BsonType representation)
  35. {
  36. _representation = representation;
  37. }
  38. /// <summary>
  39. /// Initializes a new instance of the BsonTimeSpanOptionsAttribute class.
  40. /// </summary>
  41. /// <param name="representation">The external representation.</param>
  42. /// <param name="units">The TimeSpanUnits.</param>
  43. public BsonTimeSpanOptionsAttribute(BsonType representation, TimeSpanUnits units)
  44. {
  45. _representation = representation;
  46. _units = units;
  47. }
  48. // public properties
  49. /// <summary>
  50. /// Gets the external representation.
  51. /// </summary>
  52. public BsonType Representation
  53. {
  54. get { return _representation; }
  55. }
  56. /// <summary>
  57. /// Gets or sets the TimeSpanUnits.
  58. /// </summary>
  59. public TimeSpanUnits Units
  60. {
  61. get { return _units; }
  62. set { _units = value; }
  63. }
  64. // protected methods
  65. /// <summary>
  66. /// Reconfigures the specified serializer by applying this attribute to it.
  67. /// </summary>
  68. /// <param name="serializer">The serializer.</param>
  69. /// <returns>A reconfigured serializer.</returns>
  70. protected override IBsonSerializer Apply(IBsonSerializer serializer)
  71. {
  72. var timeSpanSerializer = serializer as TimeSpanSerializer;
  73. if (timeSpanSerializer != null)
  74. {
  75. return timeSpanSerializer.WithRepresentation(_representation, _units);
  76. }
  77. return base.Apply(serializer);
  78. }
  79. }
  80. }