ReadWriteMemberFinderConvention.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.Reflection;
  16. namespace MongoDB.Bson.Serialization.Conventions
  17. {
  18. /// <summary>
  19. /// A convention that finds readable and writeable members and adds them to the class map.
  20. /// </summary>
  21. public class ReadWriteMemberFinderConvention : ConventionBase, IClassMapConvention
  22. {
  23. // private fields
  24. private readonly BindingFlags _bindingFlags;
  25. private readonly MemberTypes _memberTypes;
  26. // constructors
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="ReadWriteMemberFinderConvention" /> class.
  29. /// </summary>
  30. public ReadWriteMemberFinderConvention()
  31. : this(BindingFlags.Instance | BindingFlags.Public)
  32. { }
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="ReadWriteMemberFinderConvention" /> class.
  35. /// </summary>
  36. /// <param name="memberTypes">The member types.</param>
  37. public ReadWriteMemberFinderConvention(MemberTypes memberTypes)
  38. : this(memberTypes, BindingFlags.Instance | BindingFlags.Public)
  39. { }
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="ReadWriteMemberFinderConvention" /> class.
  42. /// </summary>
  43. /// <param name="bindingFlags">The binding flags.</param>
  44. public ReadWriteMemberFinderConvention(BindingFlags bindingFlags)
  45. : this(MemberTypes.Field | MemberTypes.Property, bindingFlags)
  46. { }
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="ReadWriteMemberFinderConvention" /> class.
  49. /// </summary>
  50. /// <param name="memberTypes">The member types.</param>
  51. /// <param name="bindingFlags">The binding flags.</param>
  52. public ReadWriteMemberFinderConvention(MemberTypes memberTypes, BindingFlags bindingFlags)
  53. {
  54. _memberTypes = memberTypes;
  55. _bindingFlags = bindingFlags | BindingFlags.DeclaredOnly;
  56. }
  57. // public methods
  58. /// <summary>
  59. /// Applies a modification to the class map.
  60. /// </summary>
  61. /// <param name="classMap">The class map.</param>
  62. public void Apply(BsonClassMap classMap)
  63. {
  64. // order is important for backwards compatibility and GetMembers changes the order of finding things.
  65. // hence, we'll check member types explicitly instead of letting GetMembers handle it.
  66. if ((_memberTypes & MemberTypes.Field) == MemberTypes.Field)
  67. {
  68. var fields = classMap.ClassType.GetTypeInfo().GetFields(_bindingFlags);
  69. foreach (var field in fields)
  70. {
  71. MapField(classMap, field);
  72. }
  73. }
  74. if ((_memberTypes & MemberTypes.Property) == MemberTypes.Property)
  75. {
  76. var properties = classMap.ClassType.GetTypeInfo().GetProperties(_bindingFlags);
  77. foreach (var property in properties)
  78. {
  79. MapProperty(classMap, property);
  80. }
  81. }
  82. }
  83. // private methods
  84. private void MapField(BsonClassMap classMap, FieldInfo fieldInfo)
  85. {
  86. if (fieldInfo.IsInitOnly || fieldInfo.IsLiteral)
  87. {
  88. // we can't write
  89. return;
  90. }
  91. classMap.MapMember(fieldInfo);
  92. }
  93. private void MapProperty(BsonClassMap classMap, PropertyInfo propertyInfo)
  94. {
  95. if (!propertyInfo.CanRead || !propertyInfo.CanWrite)
  96. {
  97. // only read and write properties are mapped
  98. return;
  99. }
  100. // skip indexers
  101. if (propertyInfo.GetIndexParameters().Length != 0)
  102. {
  103. return;
  104. }
  105. // skip overridden properties (they are already included by the base class)
  106. var getMethodInfo = propertyInfo.GetMethod;
  107. if (getMethodInfo.IsVirtual && getMethodInfo.GetBaseDefinition().DeclaringType != classMap.ClassType)
  108. {
  109. return;
  110. }
  111. classMap.MapMember(propertyInfo);
  112. }
  113. }
  114. }