NamedIdMemberConvention.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 id member by name.
  23. /// </summary>
  24. public class NamedIdMemberConvention : 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 <see cref="NamedIdMemberConvention" /> class.
  33. /// </summary>
  34. /// <param name="names">The names.</param>
  35. public NamedIdMemberConvention(params string[] names)
  36. : this((IEnumerable<string>)names)
  37. { }
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="NamedIdMemberConvention" /> class.
  40. /// </summary>
  41. /// <param name="names">The names.</param>
  42. public NamedIdMemberConvention(IEnumerable<string> names)
  43. : this(names, BindingFlags.Instance | BindingFlags.Public)
  44. { }
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="NamedIdMemberConvention" /> class.
  47. /// </summary>
  48. /// <param name="names">The names.</param>
  49. /// <param name="memberTypes">The member types.</param>
  50. public NamedIdMemberConvention(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="NamedIdMemberConvention" /> class.
  55. /// </summary>
  56. /// <param name="names">The names.</param>
  57. /// <param name="bindingFlags">The binding flags.</param>
  58. public NamedIdMemberConvention(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="NamedIdMemberConvention" /> 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 NamedIdMemberConvention(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. if (IsValidIdMember(classMap, member))
  92. {
  93. classMap.MapIdMember(member);
  94. return;
  95. }
  96. }
  97. }
  98. }
  99. private bool IsValidIdMember(BsonClassMap classMap, MemberInfo member)
  100. {
  101. if (member is PropertyInfo)
  102. {
  103. var getMethodInfo = ((PropertyInfo)member).GetMethod;
  104. if (getMethodInfo.IsVirtual && getMethodInfo.GetBaseDefinition().DeclaringType != classMap.ClassType)
  105. {
  106. return false;
  107. }
  108. }
  109. return true;
  110. }
  111. }
  112. }