DelegateClassMapConvention.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. namespace MongoDB.Bson.Serialization.Conventions
  17. {
  18. /// <summary>
  19. /// A class map convention that wraps a delegate.
  20. /// </summary>
  21. public class DelegateClassMapConvention : ConventionBase, IClassMapConvention
  22. {
  23. // private fields
  24. private readonly Action<BsonClassMap> _action;
  25. // constructors
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="DelegateClassMapConvention" /> class.
  28. /// </summary>
  29. /// <param name="name">The name.</param>
  30. /// <param name="action">The delegate.</param>
  31. public DelegateClassMapConvention(string name, Action<BsonClassMap> action)
  32. : base(name)
  33. {
  34. if (action == null)
  35. {
  36. throw new ArgumentNullException("action");
  37. }
  38. _action = action;
  39. }
  40. // public methods
  41. /// <summary>
  42. /// Applies a modification to the class map.
  43. /// </summary>
  44. /// <param name="classMap">The class map.</param>
  45. public void Apply(BsonClassMap classMap)
  46. {
  47. _action(classMap);
  48. }
  49. }
  50. }