SerializerHelper.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* Copyright 2015-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.Text;
  19. using System.Threading.Tasks;
  20. using MongoDB.Bson.Serialization;
  21. using MongoDB.Bson.Serialization.Serializers;
  22. namespace MongoDB.Driver.Linq
  23. {
  24. internal static class SerializerHelper
  25. {
  26. public static IBsonSerializer CreateArraySerializer(IBsonSerializer serializer)
  27. {
  28. return (IBsonSerializer)Activator.CreateInstance(
  29. typeof(ArraySerializer<>).MakeGenericType(serializer.ValueType),
  30. serializer);
  31. }
  32. public static IBsonSerializer CreateEnumerableSerializer(IBsonSerializer itemSerializer)
  33. {
  34. var listSerializer = CreateListSerializer(itemSerializer);
  35. return (IBsonSerializer)Activator.CreateInstance(
  36. typeof(ImpliedImplementationInterfaceSerializer<,>).MakeGenericType(
  37. typeof(IEnumerable<>).MakeGenericType(itemSerializer.ValueType),
  38. listSerializer.ValueType),
  39. listSerializer);
  40. }
  41. public static IBsonSerializer CreateHashSetSerializer(IBsonSerializer itemSerializer)
  42. {
  43. var implementationType = typeof(HashSet<>).MakeGenericType(itemSerializer.ValueType);
  44. return (IBsonSerializer)Activator.CreateInstance(
  45. typeof(EnumerableInterfaceImplementerSerializer<,>).MakeGenericType(
  46. implementationType,
  47. itemSerializer.ValueType),
  48. itemSerializer);
  49. }
  50. public static IBsonSerializer CreateListSerializer(IBsonSerializer itemSerializer)
  51. {
  52. var implementationType = typeof(List<>).MakeGenericType(itemSerializer.ValueType);
  53. return (IBsonSerializer)Activator.CreateInstance(
  54. typeof(EnumerableInterfaceImplementerSerializer<>).MakeGenericType(
  55. implementationType),
  56. itemSerializer);
  57. }
  58. public static IBsonSerializer RecursiveConfigureChildSerializer(IChildSerializerConfigurable configurable, IBsonSerializer childSerializer)
  59. {
  60. var childConfigurable = configurable.ChildSerializer as IChildSerializerConfigurable;
  61. if (childConfigurable != null)
  62. {
  63. childSerializer = RecursiveConfigureChildSerializer(childConfigurable, childSerializer);
  64. }
  65. return configurable.WithChildSerializer(childSerializer);
  66. }
  67. public static IBsonSerializer GetDeepestChildSerializer(IChildSerializerConfigurable childConfigurableSerializer)
  68. {
  69. IBsonSerializer deepestChildSerializer = null;
  70. while (childConfigurableSerializer != null)
  71. {
  72. deepestChildSerializer = childConfigurableSerializer.ChildSerializer;
  73. childConfigurableSerializer = deepestChildSerializer as IChildSerializerConfigurable;
  74. }
  75. return deepestChildSerializer;
  76. }
  77. }
  78. }