internal IBsonSerializer CreateSerializer(Type type)
{
if (type.ContainsGenericParameters)
{
var message = "Cannot create a serializer because the type to serialize is an open generic type.";
throw new InvalidOperationException(message);
}
if (_serializerType.ContainsGenericParameters && !type.IsGenericType)
{
var message = "Cannot create a serializer because the serializer type is an open generic type and the type to serialize is not generic.";
throw new InvalidOperationException(message);
}
if (_serializerType.ContainsGenericParameters)
{
var genericArguments = type.GetGenericArguments();
var closedSerializerType = _serializerType.MakeGenericType(genericArguments);
return (IBsonSerializer)Activator.CreateInstance(closedSerializerType);
}
else
{
return (IBsonSerializer)Activator.CreateInstance(_serializerType);
}
}
}