NamedExtraElementsMemberConvention.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.Linq;
  18. using System.Reflection;
  19. namespace MongoDB.Bson.Serialization.Conventions
  20. {
  21. /// <summary>
  22. /// A convention that finds the extra elements member by name (and that is also of type <see cref="BsonDocument"/> or <see cref="IDictionary{String, Object}"/>).
  23. /// </summary>
  24. public class NamedExtraElementsMemberConvention : ConventionBase, IClassMapConvention
  25. {
  26. // private fields
  27. private readonly IEnumerable<string> _names;
  28. private readonly MemberTypes _memberTypes;
  29. private readonly BindingFlags _bindingFlags;
  30. // constructors
  31. /// <summary>
  32. /// Initializes a new instance of the NamedExtraElementsMemberConvention class.
  33. /// </summary>
  34. /// <param name="name">The name of the extra elements member.</param>
  35. public NamedExtraElementsMemberConvention(string name)
  36. : this(new [] { name })
  37. { }
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="NamedExtraElementsMemberConvention" /> class.
  40. /// </summary>
  41. /// <param name="names">The names.</param>
  42. public NamedExtraElementsMemberConvention(IEnumerable<string> names)
  43. : this(names, BindingFlags.Instance | BindingFlags.Public)
  44. { }
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="NamedExtraElementsMemberConvention" /> class.
  47. /// </summary>
  48. /// <param name="names">The names.</param>
  49. /// <param name="memberTypes">The member types.</param>
  50. public NamedExtraElementsMemberConvention(IEnumerable<string> names, MemberTypes memberTypes)
  51. : this(names, memberTypes, BindingFlags.Instance | BindingFlags.Public)
  52. { }
  53. /// <summary>
  54. /// Initializes a new instance of the <see cref="NamedExtraElementsMemberConvention" /> class.
  55. /// </summary>
  56. /// <param name="names">The names.</param>
  57. /// <param name="bindingFlags">The binding flags.</param>
  58. public NamedExtraElementsMemberConvention(IEnumerable<string> names, BindingFlags bindingFlags)
  59. : this(names, MemberTypes.Field | MemberTypes.Property, bindingFlags)
  60. { }
  61. /// <summary>
  62. /// Initializes a new instance of the <see cref="NamedExtraElementsMemberConvention" /> class.
  63. /// </summary>
  64. /// <param name="names">The names.</param>
  65. /// <param name="memberTypes">The member types.</param>
  66. /// <param name="bindingFlags">The binding flags.</param>
  67. /// <exception cref="System.ArgumentNullException"></exception>
  68. public NamedExtraElementsMemberConvention(IEnumerable<string> names, MemberTypes memberTypes, BindingFlags bindingFlags)
  69. {
  70. if (names == null)
  71. {
  72. throw new ArgumentNullException("names");
  73. }
  74. _names = names;
  75. _memberTypes = memberTypes;
  76. _bindingFlags = bindingFlags | BindingFlags.DeclaredOnly;
  77. }
  78. // public methods
  79. /// <summary>
  80. /// Applies a modification to the class map.
  81. /// </summary>
  82. /// <param name="classMap">The class map.</param>
  83. public void Apply(BsonClassMap classMap)
  84. {
  85. var classTypeInfo = classMap.ClassType.GetTypeInfo();
  86. foreach (var name in _names)
  87. {
  88. var member = classTypeInfo.GetMember(name, _memberTypes, _bindingFlags).SingleOrDefault();
  89. if (member != null)
  90. {
  91. Type memberType = null;
  92. var fieldInfo = member as FieldInfo;
  93. if (fieldInfo != null)
  94. {
  95. memberType = fieldInfo.FieldType;
  96. }
  97. else
  98. {
  99. var propertyInfo = member as PropertyInfo;
  100. if (propertyInfo != null)
  101. {
  102. memberType = propertyInfo.PropertyType;
  103. }
  104. }
  105. if (memberType != null && (memberType == typeof(BsonDocument) || typeof(IDictionary<string, object>).GetTypeInfo().IsAssignableFrom(memberType)))
  106. {
  107. classMap.MapExtraElementsMember(member);
  108. return;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. }