BsonSerializationProviderBase.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.Reflection;
  17. namespace MongoDB.Bson.Serialization
  18. {
  19. /// <summary>
  20. /// Base class for serialization providers.
  21. /// </summary>
  22. public abstract class BsonSerializationProviderBase : IRegistryAwareBsonSerializationProvider
  23. {
  24. /// <inheritdoc/>
  25. public virtual IBsonSerializer GetSerializer(Type type)
  26. {
  27. return GetSerializer(type, BsonSerializer.SerializerRegistry);
  28. }
  29. /// <inheritdoc/>
  30. public abstract IBsonSerializer GetSerializer(Type type, IBsonSerializerRegistry serializerRegistry);
  31. /// <summary>
  32. /// Creates the serializer from a serializer type definition and type arguments.
  33. /// </summary>
  34. /// <param name="serializerTypeDefinition">The serializer type definition.</param>
  35. /// <param name="typeArguments">The type arguments.</param>
  36. /// <returns>A serializer.</returns>
  37. protected virtual IBsonSerializer CreateGenericSerializer(Type serializerTypeDefinition, params Type[] typeArguments)
  38. {
  39. return CreateGenericSerializer(serializerTypeDefinition, typeArguments, BsonSerializer.SerializerRegistry);
  40. }
  41. /// <summary>
  42. /// Creates the serializer from a serializer type definition and type arguments.
  43. /// </summary>
  44. /// <param name="serializerTypeDefinition">The serializer type definition.</param>
  45. /// <param name="typeArguments">The type arguments.</param>
  46. /// <param name="serializerRegistry">The serializer registry.</param>
  47. /// <returns>
  48. /// A serializer.
  49. /// </returns>
  50. protected virtual IBsonSerializer CreateGenericSerializer(Type serializerTypeDefinition, Type[] typeArguments, IBsonSerializerRegistry serializerRegistry)
  51. {
  52. var serializerType = serializerTypeDefinition.MakeGenericType(typeArguments);
  53. return CreateSerializer(serializerType, serializerRegistry);
  54. }
  55. /// <summary>
  56. /// Creates the serializer.
  57. /// </summary>
  58. /// <param name="serializerType">The serializer type.</param>
  59. /// <returns>A serializer.</returns>
  60. protected virtual IBsonSerializer CreateSerializer(Type serializerType)
  61. {
  62. return CreateSerializer(serializerType, BsonSerializer.SerializerRegistry);
  63. }
  64. /// <summary>
  65. /// Creates the serializer.
  66. /// </summary>
  67. /// <param name="serializerType">The serializer type.</param>
  68. /// <param name="serializerRegistry">The serializer registry.</param>
  69. /// <returns>
  70. /// A serializer.
  71. /// </returns>
  72. protected virtual IBsonSerializer CreateSerializer(Type serializerType, IBsonSerializerRegistry serializerRegistry)
  73. {
  74. var serializerTypeInfo = serializerType.GetTypeInfo();
  75. var constructorInfo = serializerTypeInfo.GetConstructor(new[] { typeof(IBsonSerializerRegistry) });
  76. if (constructorInfo != null)
  77. {
  78. return (IBsonSerializer)constructorInfo.Invoke(new object[] { serializerRegistry });
  79. }
  80. constructorInfo = serializerTypeInfo.GetConstructor(new Type[0]);
  81. if (constructorInfo != null)
  82. {
  83. return (IBsonSerializer)constructorInfo.Invoke(new object[0]);
  84. }
  85. throw new MissingMethodException(string.Format(
  86. "No suitable constructor found for serializer type: '{0}'.",
  87. serializerType.FullName));
  88. }
  89. }
  90. }