ConventionBase.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. /// Base class for a convention.
  20. /// </summary>
  21. public abstract class ConventionBase : IConvention
  22. {
  23. // private fields
  24. private readonly string _name;
  25. // constructors
  26. /// <summary>
  27. /// Initializes a new instance of the ConventionBase class.
  28. /// </summary>
  29. protected ConventionBase()
  30. {
  31. _name = GetName(this.GetType());
  32. }
  33. /// <summary>
  34. /// Initializes a new instance of the ConventionBase class.
  35. /// </summary>
  36. /// <param name="name">The name of the convention.</param>
  37. protected ConventionBase(string name)
  38. {
  39. if (name == null)
  40. {
  41. throw new ArgumentNullException("name");
  42. }
  43. _name = name;
  44. }
  45. // public properties
  46. /// <summary>
  47. /// Gets the name of the convention.
  48. /// </summary>
  49. public string Name
  50. {
  51. get { return _name; }
  52. }
  53. // private static methods
  54. private static string GetName(Type type)
  55. {
  56. var name = type.Name;
  57. if (name.EndsWith("Convention"))
  58. {
  59. return name.Substring(0, name.Length - 10);
  60. }
  61. return name;
  62. }
  63. }
  64. }