| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- /* Copyright 2010-present MongoDB Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- using System;
- using System.Reflection;
- namespace MongoDB.Bson.Serialization
- {
- /// <summary>
- /// Base class for serialization providers.
- /// </summary>
- public abstract class BsonSerializationProviderBase : IRegistryAwareBsonSerializationProvider
- {
- /// <inheritdoc/>
- public virtual IBsonSerializer GetSerializer(Type type)
- {
- return GetSerializer(type, BsonSerializer.SerializerRegistry);
- }
- /// <inheritdoc/>
- public abstract IBsonSerializer GetSerializer(Type type, IBsonSerializerRegistry serializerRegistry);
- /// <summary>
- /// Creates the serializer from a serializer type definition and type arguments.
- /// </summary>
- /// <param name="serializerTypeDefinition">The serializer type definition.</param>
- /// <param name="typeArguments">The type arguments.</param>
- /// <returns>A serializer.</returns>
- protected virtual IBsonSerializer CreateGenericSerializer(Type serializerTypeDefinition, params Type[] typeArguments)
- {
- return CreateGenericSerializer(serializerTypeDefinition, typeArguments, BsonSerializer.SerializerRegistry);
- }
- /// <summary>
- /// Creates the serializer from a serializer type definition and type arguments.
- /// </summary>
- /// <param name="serializerTypeDefinition">The serializer type definition.</param>
- /// <param name="typeArguments">The type arguments.</param>
- /// <param name="serializerRegistry">The serializer registry.</param>
- /// <returns>
- /// A serializer.
- /// </returns>
- protected virtual IBsonSerializer CreateGenericSerializer(Type serializerTypeDefinition, Type[] typeArguments, IBsonSerializerRegistry serializerRegistry)
- {
- var serializerType = serializerTypeDefinition.MakeGenericType(typeArguments);
- return CreateSerializer(serializerType, serializerRegistry);
- }
- /// <summary>
- /// Creates the serializer.
- /// </summary>
- /// <param name="serializerType">The serializer type.</param>
- /// <returns>A serializer.</returns>
- protected virtual IBsonSerializer CreateSerializer(Type serializerType)
- {
- return CreateSerializer(serializerType, BsonSerializer.SerializerRegistry);
- }
- /// <summary>
- /// Creates the serializer.
- /// </summary>
- /// <param name="serializerType">The serializer type.</param>
- /// <param name="serializerRegistry">The serializer registry.</param>
- /// <returns>
- /// A serializer.
- /// </returns>
- protected virtual IBsonSerializer CreateSerializer(Type serializerType, IBsonSerializerRegistry serializerRegistry)
- {
- var serializerTypeInfo = serializerType.GetTypeInfo();
- var constructorInfo = serializerTypeInfo.GetConstructor(new[] { typeof(IBsonSerializerRegistry) });
- if (constructorInfo != null)
- {
- return (IBsonSerializer)constructorInfo.Invoke(new object[] { serializerRegistry });
- }
- constructorInfo = serializerTypeInfo.GetConstructor(new Type[0]);
- if (constructorInfo != null)
- {
- return (IBsonSerializer)constructorInfo.Invoke(new object[0]);
- }
- throw new MissingMethodException(string.Format(
- "No suitable constructor found for serializer type: '{0}'.",
- serializerType.FullName));
- }
- }
- }
|